migration.custom.class.php 146 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173
  1. <?php
  2. /**
  3. * This file contains the MigrationCustom class, which defines methods to
  4. * alter the data from the original database when importing it to the Chamilo
  5. * database
  6. */
  7. /**
  8. * The custom migration class allows you to define rules to process data
  9. * during the migration
  10. */
  11. class MigrationCustom {
  12. const default_admin_id = 1;
  13. const TRANSACTION_STATUS_TO_BE_EXECUTED = 1;
  14. const TRANSACTION_STATUS_SUCCESSFUL = 2;
  15. const TRANSACTION_STATUS_DEPRECATED = 3; //??
  16. const TRANSACTION_STATUS_FAILED = 4;
  17. const TRANSACTION_STATUS_ABANDONNED = 5;
  18. /**
  19. * Types of transaction operations read from the external databases
  20. */
  21. const TRANSACTION_TYPE_ADD_USER = 1;
  22. const TRANSACTION_TYPE_DEL_USER = 2;
  23. const TRANSACTION_TYPE_EDIT_USER = 3;
  24. const TRANSACTION_TYPE_SUB_USER = 4; //subscribe user to a session
  25. const TRANSACTION_TYPE_ADD_COURSE = 5;
  26. const TRANSACTION_TYPE_DEL_COURSE = 6;
  27. const TRANSACTION_TYPE_EDIT_COURSE = 7;
  28. const TRANSACTION_TYPE_ADD_SESS = 8;
  29. const TRANSACTION_TYPE_DEL_SESS = 9;
  30. const TRANSACTION_TYPE_EDIT_SESS = 10;
  31. const TRANSACTION_TYPE_UPD_ROOM = 11;
  32. const TRANSACTION_TYPE_UPD_SCHED = 12;
  33. const TRANSACTION_TYPE_ADD_SCHED = 13;
  34. const TRANSACTION_TYPE_DEL_SCHED = 14;
  35. const TRANSACTION_TYPE_EDIT_SCHED = 15;
  36. const TRANSACTION_TYPE_ADD_ROOM = 16;
  37. const TRANSACTION_TYPE_DEL_ROOM = 17;
  38. const TRANSACTION_TYPE_EDIT_ROOM = 18;
  39. const TRANSACTION_TYPE_ADD_BRANCH = 19;
  40. const TRANSACTION_TYPE_DEL_BRANCH = 20;
  41. const TRANSACTION_TYPE_EDIT_BRANCH = 21;
  42. const TRANSACTION_TYPE_ADD_FREQ = 22;
  43. const TRANSACTION_TYPE_DEL_FREQ = 23;
  44. const TRANSACTION_TYPE_EDIT_FREQ = 24;
  45. const TRANSACTION_TYPE_ADD_INTENS = 25;
  46. const TRANSACTION_TYPE_DEL_INTENS = 26;
  47. const TRANSACTION_TYPE_EDIT_INTENS = 27;
  48. const TRANSACTION_TYPE_ADD_FASE = 28;
  49. const TRANSACTION_TYPE_DEL_FASE = 29;
  50. const TRANSACTION_TYPE_EDIT_FASE = 30;
  51. //Gradebook
  52. const TRANSACTION_TYPE_ADD_NOTA = 31;
  53. const TRANSACTION_TYPE_DEL_NOTA = 32;
  54. const TRANSACTION_TYPE_EDIT_NOTA = 33;
  55. //Attendances
  56. const TRANSACTION_TYPE_ADD_ASSIST = 34;
  57. const TRANSACTION_TYPE_DEL_ASSIST = 35;
  58. const TRANSACTION_TYPE_EDIT_ASSIST = 36;
  59. public static $attend_status = array(
  60. 'DEF' => 4,
  61. 'AUS' => 0,
  62. 'PRE' => 1,
  63. 'TAR' => 2,
  64. 'T45' => 3,
  65. );
  66. static function get_transaction_status_list() {
  67. $table = Database::get_main_table(TABLE_BRANCH_TRANSACTION_STATUS);
  68. return Database::select("*", $table);
  69. }
  70. /**
  71. * The only required method is the 'none' method, which will not trigger
  72. * any process at all
  73. * @param mixed Data
  74. * @param mixed Unaltered data
  75. */
  76. static function none($data) {
  77. return $data;
  78. }
  79. static function join_horario($data, &$omigrate, $row_data) {
  80. return '('.$row_data['chrIdHorario'].') '.$row_data['chrHoraInicial'].' '.$row_data['chrHoraFinal'];
  81. }
  82. /**
  83. * Converts 2008-02-01 12:20:20.540 to 2008-02-01 12:20:20
  84. */
  85. static function clean_date_time($date) {
  86. return substr($date, 0, 19);
  87. }
  88. /* Converts 2009-09-30T00:00:00-05:00 to 2009-09-30 00:00:00*/
  89. static function clean_date_time_from_ws($date) {
  90. $pre_clean = self::clean_date_time($date, 0, 19);
  91. return str_replace('T', ' ', $pre_clean);
  92. }
  93. /**
  94. * Transform the uid identifiers from MSSQL to a string
  95. * @param string Field name
  96. * @return string SQL select string to include in the final select
  97. */
  98. static function sql_alter_unhash_50($field) {
  99. $as_field = explode('.', $field);
  100. if (isset($as_field[1])) {
  101. $as_field = $as_field[1];
  102. } else {
  103. $as_field = $field;
  104. }
  105. return " cast( $field as varchar(50)) as $as_field ";
  106. }
  107. static function clean_utf8($value) {
  108. return utf8_encode($value);
  109. }
  110. static function make_sha1($value) {
  111. return sha1($value);
  112. }
  113. static function add_meses_label_to_extra_field_fase($value, $data, $row_data) {
  114. $label = 'meses';
  115. if ($row_data['chrOrdenFase'] == 1) {
  116. $label = 'mes';
  117. }
  118. $value = $row_data['vchNombreFase'] .' ['.trim($row_data['chrOrdenFase']).' '.$label.']';
  119. return self::clean_utf8($value);
  120. }
  121. static function clean_session_name($value, &$omigrate, $row_data) {
  122. return self::clean_utf8($row_data['session_name']);
  123. }
  124. static function get_real_course_code($data, &$omigrate=null) {
  125. if (is_array($omigrate) && $omigrate['boost_courses']) {
  126. if (isset($omigrate['courses'][$data])) {
  127. return $omigrate['courses'][$data];
  128. }
  129. }
  130. $extra_field = new ExtraFieldValue('course');
  131. $data = strtoupper($data);
  132. $values = $extra_field->get_item_id_from_field_variable_and_field_value('uidIdCurso', $data);
  133. if ($values) {
  134. return $values['course_code'];
  135. } else {
  136. error_log("Course ".print_r($data,1)." not found in DB");
  137. }
  138. }
  139. static function get_session_id_by_programa_id($uidIdPrograma, &$omigrate=null) {
  140. if (is_array($omigrate) && $omigrate['boost_sessions']) {
  141. if (isset($omigrate['sessions'][$uidIdPrograma])) {
  142. return $omigrate['sessions'][$uidIdPrograma];
  143. }
  144. }
  145. $extra_field = new ExtraFieldValue('session');
  146. $uidIdPrograma = strtoupper($uidIdPrograma);
  147. $values = $extra_field->get_item_id_from_field_variable_and_field_value('uidIdPrograma', $uidIdPrograma);
  148. if ($values) {
  149. return $values['session_id'];
  150. } else {
  151. //error_log("session id not found in DB");
  152. }
  153. }
  154. /* Not used */
  155. static function get_user_id_by_persona_id($uidIdPersona, &$omigrate=null) {
  156. if (is_array($omigrate) && $omigrate['boost_users']) {
  157. if (isset($omigrate['users'][$uidIdPersona])) {
  158. return $omigrate['users'][$uidIdPersona];
  159. }
  160. }
  161. //error_log('get_user_id_by_persona_id');
  162. $extra_field = new ExtraFieldValue('user');
  163. $uidIdPersona = strtoupper($uidIdPersona);
  164. $values = $extra_field->get_item_id_from_field_variable_and_field_value('uidIdPersona', $uidIdPersona);
  165. if ($values) {
  166. return $values['user_id'];
  167. } else {
  168. return 0;
  169. }
  170. }
  171. static function get_real_teacher_id($uidIdPersona, &$omigrate=null) {
  172. $default_teacher_id = self::default_admin_id;
  173. if (empty($uidIdPersona)) {
  174. //error_log('No teacher provided');
  175. return $default_teacher_id;
  176. }
  177. if (is_array($omigrate) && $omigrate['boost_users']) {
  178. if (isset($omigrate['users'][$uidIdPersona])) {
  179. return $omigrate['users'][$uidIdPersona];
  180. }
  181. }
  182. $extra_field = new ExtraFieldValue('user');
  183. $values = $extra_field->get_item_id_from_field_variable_and_field_value('uidIdPersona', $uidIdPersona);
  184. if ($values) {
  185. return $values['user_id'];
  186. } else {
  187. return $default_teacher_id;
  188. }
  189. }
  190. /**
  191. * Manage the user creation, including checking if the user hasn't been
  192. * created previously
  193. * @param array User data
  194. * @param object List of migrated things
  195. * @return array User info (from Chamilo DB)
  196. */
  197. static function create_user($data, &$omigrate=null) {
  198. //error_log('In create_user, receiving '.print_r($data,1));
  199. if (empty($data['uidIdPersona'])) {
  200. error_log('User does not have a uidIdPersona');
  201. error_log(print_r($data, 1));
  202. return false;
  203. //exit;
  204. }
  205. $data['uidIdPersona'] = strtoupper($data['uidIdPersona']);
  206. $data['status'] = STUDENT;
  207. if (isset($data['uidIdEmpleado'])) {
  208. $data['status'] = COURSEMANAGER;
  209. }
  210. if (!isset($data['username']) || empty($data['username'])) {
  211. $data['firstname'] = (string) trim($data['firstname']);
  212. $data['lastname'] = (string) trim($data['lastname']);
  213. if (empty($data['firstname']) && empty($data['lastname'])) {
  214. $wanted_user_name = UserManager::purify_username($data['uidIdPersona']);
  215. //$wanted_user_name = UserManager::create_unique_username(null, null);
  216. } else {
  217. $wanted_user_name = UserManager::create_username($data['firstname'], $data['lastname']);
  218. }
  219. $extra_data = UserManager::get_extra_user_data_by_value('uidIdPersona', $data['uidIdPersona']);
  220. if ($extra_data) {
  221. $user_info = api_get_user_info($extra_data[0]);
  222. //print_r($extra_data);
  223. //error_log("User_already_added - {$user_info['user_id']} - {$user_info['username']} - {$user_info['firstname']} - {$user_info['lastname']}");
  224. return $user_info;
  225. }
  226. if (UserManager::is_username_available($wanted_user_name)) {
  227. $data['username'] = $wanted_user_name;
  228. error_log("username available $wanted_user_name");
  229. } else {
  230. //the user already exists?
  231. $user_info = UserManager::get_user_info_simple($wanted_user_name);
  232. $user_persona = UserManager::get_extra_user_data_by_field($user_info['user_id'], 'uidIdPersona');
  233. if (isset($user_persona['uidIdPersona']) && $data['uidIdPersona'] == $user_persona['uidIdPersona']) {
  234. error_log("Skip user already added: {$user_info['username']}");
  235. return $user_info;
  236. } else {
  237. error_log("Homonym - wanted_username: $wanted_user_name - uidIdPersona: {$user_persona['uidIdPersona']} - username: {$user_info['username']}");
  238. //print_r($data);
  239. //The user has the same firstname and lastname but it has another uiIdPersona could by an homonym
  240. $data['username'] = UserManager::create_unique_username($data['firstname'], $data['lastname']);
  241. error_log("homonym username created ". $data['username']);
  242. }
  243. }
  244. if (empty($data['username'])) {
  245. //Last chance to have a nice username
  246. if (empty($data['firstname']) && empty($data['lastname'])) {
  247. $data['username'] = UserManager::create_unique_username(uniqid());
  248. error_log("username empty 1". $data['username']);
  249. } else {
  250. $data['username'] = UserManager::create_unique_username($data['firstname'], $data['lastname']);
  251. error_log("username empty 2". $data['username']);
  252. }
  253. }
  254. } else {
  255. if (UserManager::is_username_available($data['username'])) {
  256. //error_log("username available {$data['username']} ");
  257. } else {
  258. //the user already exists?
  259. $user_info = UserManager::get_user_info_simple($data['username']);
  260. $user_persona = UserManager::get_extra_user_data_by_field($user_info['user_id'], 'uidIdPersona');
  261. if (isset($user_persona['uidIdPersona']) && (string)$data['uidIdPersona'] == (string)$user_persona['uidIdPersona']) {
  262. //error_log("2 Skip user already added: {$user_info['username']}");
  263. return $user_info;
  264. } else {
  265. //print_r($user_persona);
  266. //error_log("2 homonym - wanted_username: {$data['username']} - uidIdPersona: {$user_persona['uidIdPersona']} - username: {$user_info['username']}");
  267. //print_r($data);
  268. //The user has the same firstname and lastname but it has another uiIdPersona could by an homonym
  269. $data['username'] = UserManager::create_unique_username($data['firstname'], $data['lastname']);
  270. //error_log("2 homonym username created ". $data['username']);
  271. }
  272. }
  273. }
  274. if (empty($data['username'])) {
  275. error_log('No Username provided');
  276. error_log(print_r($data, 1));
  277. return false;
  278. //exit;
  279. }
  280. $id_persona = $data['uidIdPersona'];
  281. unset($data['uidIdPersona']);
  282. unset($data['uidIdAlumno']);
  283. unset($data['uidIdEmpleado']);
  284. $data['encrypt_method'] = 'sha1';
  285. global $api_failureList;
  286. $api_failureList = array();
  287. //error_log(print_r($data, 1));
  288. $user_info = UserManager::add($data);
  289. if (!$user_info) {
  290. error_log('User '.$id_persona.' could not be inserted (maybe duplicate?)');
  291. } else {
  292. //error_log('User '.$id_persona.' was created as user '.$user_info['user_id']);
  293. }
  294. if (is_array($omigrate) && isset($omigrate) && $omigrate['boost_users']) {
  295. $omigrate['users'][$id_persona] = $user_info['user_id'];
  296. }
  297. UserManager::update_extra_field_value($user_info['user_id'], 'uidIdPersona', $id_persona);
  298. return $user_info;
  299. }
  300. /**
  301. * Manages the course creation based on the rules in db_matches.php
  302. */
  303. static function create_course($data, &$omigrate=null) {
  304. //error_log('In create_course, received '.print_r($data,1));
  305. //Fixes wrong wanted codes
  306. $data['wanted_code'] = str_replace(array('-', '_'), '000', $data['wanted_code']);
  307. //Specific to customer, set the default language to English
  308. $data['language'] = 'english';
  309. $data['visibility'] = COURSE_VISIBILITY_REGISTERED;
  310. //Creates an evaluation
  311. $data['create_gradebook_evaluation'] = false;
  312. /*
  313. $data['gradebook_params'] = array(
  314. 'name' => 'General evaluation',
  315. 'user_id' => self::default_admin_id,
  316. 'weight' => '20',
  317. 'max' => '20'
  318. );*/
  319. $course_data = CourseManager::create_course($data);
  320. if (is_array($omigrate) && isset($omigrate) && $omigrate['boost_courses']) {
  321. $omigrate['courses'][$data['uidIdCurso']] = $course_data['code'];
  322. }
  323. return $course_data;
  324. }
  325. /**
  326. * Manages the session creation, based on data provided by the rules
  327. * in db_matches.php
  328. * @return int The created (or existing) session ID
  329. */
  330. static function create_session($data, &$omigrate) {
  331. //Hack to add the default gradebook course to the session course
  332. $data['create_gradebook_evaluation'] = true;
  333. /*$data['gradebook_params'] = array(
  334. 'name' => 'General evaluation',
  335. 'user_id' => self::default_admin_id,
  336. 'weight' => '20',
  337. 'max' => '20'
  338. );*/
  339. self::fix_access_dates($data);
  340. // Here the $data variable has $data['course_code'] that will be added
  341. // when creating the session
  342. // If session already exists, it will return the existing session id
  343. $session_id = SessionManager::add($data, true);
  344. if (!$session_id) {
  345. error_log('Error: Failed to create_session '.$data['name']);
  346. } else{
  347. $c = SessionManager::set_coach_to_course_session($data['id_coach'], $session_id, $data['course_code']);
  348. if (is_array($omigrate) && isset($omigrate) && $omigrate['boost_sessions']) {
  349. $omigrate['sessions'][$data['uidIdPrograma']] = $session_id;
  350. }
  351. }
  352. return $session_id;
  353. }
  354. /**
  355. * Assigns a user to a session based on rules in db_matches.php
  356. */
  357. static function add_user_to_session($data, &$omigrate=null) {
  358. $session_id = null;
  359. $user_id = null;
  360. if (is_array($omigrate) && $omigrate['boost_sessions']) {
  361. if (isset($omigrate['sessions'][$data['uidIdPrograma']])) {
  362. $session_id = $omigrate['sessions'][$data['uidIdPrograma']];
  363. }
  364. }
  365. if (empty($session_id)) {
  366. $extra_field_value = new ExtraFieldValue('session');
  367. $result = $extra_field_value->get_item_id_from_field_variable_and_field_value('uidIdPrograma', $data['uidIdPrograma']);
  368. //error_log('data[uidIdPrograma] '.$data['uidIdPrograma'].' returned $result[session_id]: '.$result['session_id']);
  369. if ($result && $result['session_id']) {
  370. $session_id = $result['session_id'];
  371. }
  372. }
  373. if (is_array($omigrate) && $omigrate['boost_users']) {
  374. if (isset($omigrate['users'][$data['uidIdPersona']])) {
  375. $user_id = $omigrate['users'][$data['uidIdPersona']];
  376. }
  377. }
  378. if (empty($user_id)) {
  379. $extra_field_value = new ExtraFieldValue('user');
  380. $result = $extra_field_value->get_item_id_from_field_variable_and_field_value('uidIdPersona', $data['uidIdPersona']);
  381. //error_log('data[uidIdPersona] '.$data['uidIdPersona'].' returned $result[user_id]: '.$result['user_id']);
  382. if ($result && $result['user_id']) {
  383. $user_id = $result['user_id'];
  384. }
  385. }
  386. if (!empty($session_id) && !empty($user_id)) {
  387. //error_log('Called: add_user_to_session - Subscribing: session_id: '.$session_id. ' user_id: '.$user_id);
  388. SessionManager::suscribe_users_to_session($session_id, array($user_id), SESSION_VISIBLE_READ_ONLY, false, false);
  389. //exit;
  390. } else {
  391. //error_log('Called: add_user_to_session - No idPrograma: '.$data['uidIdPrograma'].' - No uidIdPersona: '.$data['uidIdPersona']);
  392. }
  393. }
  394. static function create_attendance($data) {
  395. //error_log('create_attendance');
  396. $session_id = $data['session_id'];
  397. $user_id = $data['user_id'];
  398. $time = self::get_horario_value($session_id);
  399. $fecha = $data['fecha']." $time:00";
  400. if (!empty($session_id) && !empty($user_id)) {
  401. $attendance = new Attendance();
  402. $course_list = false;
  403. global $data_list;
  404. if (is_array($data_list) && isset($data_list) && $data_list['boost_sessions']) {
  405. $course_list = array(0 => array('code' => $data_list['session_course'][$session_id]));
  406. } else {
  407. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  408. }
  409. $attendance_sheet_id = null;
  410. if (!empty($course_list)) {
  411. //We know there's only one course by session. Take it.
  412. $course = current($course_list);
  413. //Creating attendance
  414. if (isset($course['code'])) {
  415. if (is_array($data_list) && isset($data_list) && $data_list['boost_courses']) {
  416. $course_info = array('real_id' => $data_list['course_ids'][$course['code']]);
  417. } else {
  418. $course_info = api_get_course_info($course['code']);
  419. }
  420. $attendance->set_course_id($course['code']);
  421. $attendance->set_course_int_id($course_info['real_id']);
  422. $attendance->set_session_id($session_id);
  423. if (is_array($data_list) && isset($data_list) && $data_list['boost_sessions'] && isset($data_list['sessions_attendances'][$course_info['real_id']][$session_id])) {
  424. $list = $data_list['sessions_attendances'][$course_info['real_id']][$session_id];
  425. foreach ($list as $at_id) {
  426. $attendance_list[] = array('id' => $at_id);
  427. }
  428. } else {
  429. $attendance_list = $attendance->get_attendances_list($course_info['real_id'], $session_id);
  430. }
  431. if (empty($attendance_list)) {
  432. $attendance->set_name('Asistencia');
  433. $attendance->set_description('');
  434. //$attendance->set_attendance_qualify_title($_POST['attendance_qualify_title']);
  435. //$attendance->set_attendance_weight($_POST['attendance_weight']);
  436. $link_to_gradebook = false;
  437. //$attendance->category_id = $_POST['category_id'];
  438. $attendance_sheet_id = $attendance->attendance_add($link_to_gradebook, self::default_admin_id);
  439. if (is_array($data_list) && isset($data_list) && $data_list['boost_sessions']) {
  440. $data_list['sessions_attendances'][$course_info['real_id']][$session_id][] = $attendance_sheet_id;
  441. }
  442. //error_log("Attendance added course code: {$course['code']} - session_id: $session_id");
  443. //only 1 course per session
  444. } else {
  445. // Only one attendance sheet by course and session (at least during migration)
  446. $attendance_data = current($attendance_list);
  447. $attendance_sheet_id = $attendance_data['id'];
  448. //error_log("Attendance found in attendance_sheet_id = $attendance_sheet_id - course code: {$course['code']} - session_id: $session_id");
  449. }
  450. // Now the attendance_sheet has been found or created, check the date
  451. if ($attendance_sheet_id) {
  452. //error_log('Processing attendance sheet '.$attendance_sheeet_id.' for session '.$session_id.', course '.$course_info['real_id'].', date '.$fecha);
  453. //Attendance date exists?
  454. $cal_info = array();
  455. $cal_id = null;
  456. if (is_array($data_list) && isset($data_list) && $data_list['boost_sessions'] && $data_list['sessions_attendance_dates'][$attendance_sheet_id][$fecha]) {
  457. $cal_info['id'] = $data_list['sessions_attendance_dates'][$attendance_sheet_id][$fecha];
  458. } else {
  459. $cal_info = $attendance->get_attendance_calendar_data_by_date($attendance_sheet_id, $fecha);
  460. }
  461. // Get (or create) the attendance date ID
  462. if (isset($cal_info['id'])) {
  463. $cal_id = $cal_info['id'];
  464. } else {
  465. $attendance->set_date_time($fecha);
  466. //Creating the attendance date and get ID
  467. $cal_id = $attendance->attendance_calendar_add($attendance_sheet_id, true);
  468. if (is_array($data_list) && isset($data_list) && $data_list['boost_sessions']) {
  469. $data_list['sessions_attendance_dates'][$attendance_sheet_id][$fecha] = $cal_id;
  470. }
  471. //error_log("Creating attendance calendar $cal_id");
  472. }
  473. //Adding presence for the user (by default everybody is present)
  474. $users_present = array($user_id => $data['status']);
  475. if (is_array($data_list['create_attendance'])) {
  476. $data_list['create_attendance'][] = array($cal_id, $user_id, $data['status'], $attendance_sheet_id, $course_info['real_id'], $fecha);
  477. $limit = 100;
  478. if (count($data_list['create_attendance']) == $limit) {
  479. //error_log('Flushing attendances list because reached '.$limit.": \n".print_r($data_list['create_attendance'],1));
  480. $attendance->attendance_sheet_group_add($data_list['create_attendance'],false,true);
  481. $data_list['create_attendance'] = array();
  482. }
  483. } else {
  484. $attendance->attendance_sheet_add($cal_id, $users_present, $attendance_sheet_id, false, true);
  485. }
  486. //error_log("Adding calendar to user: $user_id to calendar: $cal_id");
  487. } else {
  488. // We should never get here
  489. error_log('No attendance_sheet_id created');
  490. }
  491. } else {
  492. error_log("Course not found for session: $session_id");
  493. }
  494. }
  495. } else {
  496. error_log("Missing data: session: $session_id - user_id: $user_id");
  497. }
  498. }
  499. static function convert_attendance_status($status) {
  500. if (!in_array($status,array_keys(self::$attend_status))) { return null; }
  501. return self::$attend_status[$status];
  502. }
  503. static function create_thematic($data) {
  504. //error_log('create_thematic');
  505. $session_id = $data['session_id'];
  506. if (!empty($session_id)) {
  507. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  508. if (!empty($course_list)) {
  509. $course_data = current($course_list);
  510. $course_info = api_get_course_info($course_data['code']);
  511. if (!empty($course_data)) {
  512. $thematic = new Thematic($course_info);
  513. $thematic->set_course_int_id($course_info['real_id']);
  514. $thematic->set_session_id($session_id);
  515. $thematic_info = $thematic->get_thematic_by_title($data['thematic']);
  516. if (empty($thematic_info)) {
  517. $thematic->set_thematic_attributes(null, $data['thematic'], null, $session_id);
  518. $thematic_id = $thematic->thematic_save();
  519. error_log("Thematic added to course code: {$course_info['code']} - session_id: $session_id");
  520. } else {
  521. $thematic_id = isset($thematic_info['id']) ? $thematic_info['id'] : null;
  522. error_log("Thematic id #$thematic_id found in course: {$course_info['code']} - session_id: $session_id");
  523. }
  524. if ($thematic_id) {
  525. $thematic->set_thematic_plan_attributes($thematic_id, $data['thematic_plan'], null, 6);
  526. $thematic->thematic_plan_save();
  527. error_log("Saving plan attributes: {$data['thematic_plan']}");
  528. }
  529. error_log("Adding thematic id : $thematic_id to session: $session_id to course: {$course_info['code']} real_id: {$course_info['real_id']}");
  530. if ($thematic_id) {
  531. error_log("Thematic saved: $thematic_id");
  532. } else {
  533. error_log("Thematic NOT saved");
  534. }
  535. }
  536. //if ($course_info['code'] != 'B05') {
  537. //exit;
  538. //}
  539. } else {
  540. error_log("No courses in session $session_id ");
  541. }
  542. }
  543. }
  544. static function add_evaluation_type($params) {
  545. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION_TYPE);
  546. if (!empty($params['name']) && !empty($params['external_id'])) {
  547. if (isset($params['return_item_if_already_exists'])) {
  548. unset($params['return_item_if_already_exists']);
  549. }
  550. Database::insert($table, $params);
  551. }
  552. }
  553. static function get_evaluation_type($external_id) {
  554. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION_TYPE);
  555. $external_id = intval($external_id);
  556. $sql = "SELECT * FROM $table WHERE external_id = $external_id";
  557. $result = Database::query($sql);
  558. if (Database::num_rows($result)) {
  559. $result = Database::fetch_array($result, 'ASSOC');
  560. return $result['id'];
  561. }
  562. return false;
  563. }
  564. static function create_gradebook_evaluation($data){
  565. //error_log('create_gradebook_evaluation() function called');
  566. $session_id = isset($data['session_id']) ? $data['session_id'] : null;
  567. if (!empty($session_id)) {
  568. global $data_list;
  569. $course_list = array(0 => array('code' => $data_list['session_course'][$session_id]));
  570. //$course_list = SessionManager::get_course_list_by_session_id($session_id);
  571. if (!empty($course_list)) {
  572. $course_data = current($course_list);
  573. if (isset($course_data['code'])) {
  574. //Get gradebook (if does not exist, create it)
  575. $gradebook = null;
  576. if (!empty($data_list['session_course_gradebook'][$course_data['code']][$session_id])) {
  577. $gradebook = array('id' => $data_list['session_course_gradebook'][$course_data['code']][$session_id]);
  578. } else {
  579. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  580. $gradebook = array('id' => create_default_course_gradebook($course_data['code'], false, $session_id));
  581. error_log('Gradebook not found. Creating one with ID '.$gradebook['id']);
  582. $data_list['session_course_gradebook'][$course_data['code']][$session_id] = $gradebook['id'];
  583. }
  584. if (!empty($gradebook)) {
  585. //Check if gradebook exists
  586. $eval = 0;
  587. $evals_found = $data_list['session_course_gradebook_eval'][$course_data['code']][$data['gradebook_description']];
  588. if (!empty($evals_found)) {
  589. return null;
  590. }
  591. $eval = new Evaluation();
  592. $evals_found = $eval->load(null, null, $course_data['code'], $gradebook['id'], null, null, $data['gradebook_description']);
  593. if (empty($evals_found)) {
  594. $eval->set_name($data['gradebook_description']);
  595. $eval->set_description($data['gradebook_description']);
  596. $eval->set_evaluation_type_id($data['gradebook_evaluation_type_id']);
  597. $eval->set_user_id(self::default_admin_id);
  598. $eval->set_course_code($course_data['code']);
  599. $eval->set_category_id($gradebook['id']);
  600. //harcoded values
  601. $eval->set_weight(100);
  602. $eval->set_max(100);
  603. $eval->set_visible(1);
  604. $eval_id = $eval->add();
  605. $data_list['session_course_gradebook_eval'][$course_data['code']][$data['gradebook_description']] = $eval_id;
  606. error_log("Gradebook evaluation ID $eval_id created!!");
  607. } else {
  608. error_log("Gradebook evaluation already exists - skipping insert :/ ");
  609. }
  610. } else {
  611. error_log("Gradebook does not exists");
  612. }
  613. } else {
  614. error_log("Something is wrong with the course ");
  615. }
  616. } else {
  617. error_log("NO course found for session id: $session_id");
  618. }
  619. } else {
  620. error_log("NO session id found: $session_id");
  621. }
  622. }
  623. static function add_gradebook_result($data) {
  624. error_log('add_gradebook_result');
  625. $session_id = isset($data['session_id']) ? $data['session_id'] : null;
  626. $user_id = isset($data['user_id']) ? $data['user_id'] : null;
  627. if (!empty($session_id) && !empty($user_id)) {
  628. global $data_list, $utc_datetime;
  629. $course_list = array(0 => array('code' => $data_list['session_course'][$session_id]));
  630. //$course_list = SessionManager::get_course_list_by_session_id($session_id);
  631. if (!empty($course_list)) {
  632. $course_data = current($course_list);
  633. if (isset($course_data['code'])) {
  634. //Get gradebook
  635. $gradebook = array('id' => $data_list['session_course_gradebook'][$course_data['code']][$session_id]);
  636. //$gradebook = new Gradebook();
  637. //$gradebook = $gradebook->get_first(array('where' => array('course_code = ? AND session_id = ?' => array($course_data['code'], $session_id))));
  638. //error_log("Looking for gradebook in course code: {$course_data['code']} - session_id: $session_id, user_id: $user_id");
  639. if (!empty($gradebook)) {
  640. error_log("Gradebook exists: {$gradebook['id']}");
  641. //Check if gradebook exists
  642. $eval = 0;
  643. $eval_id = $data_list['session_course_gradebook_eval'][$course_data['code']][$data['gradebook_description']];
  644. if (empty($eval_id)) {
  645. $eval = new Evaluation();
  646. $evals_found = $eval->load(null, null, null, $gradebook['id'], null, null);
  647. if (!empty($evals_found)) {
  648. $evaluation = current($evals_found);
  649. $eval_id = $evaluation->get_id();
  650. }
  651. }
  652. //if (!empty($evals_found)) {
  653. if (!empty($eval_id)) {
  654. //$evaluation = current($evals_found);
  655. //$eval_id = $evaluation->get_id();
  656. //Eval found
  657. $check_result = $data_list['course_eval_results'][$eval_id][$user_id];
  658. //$res = new Result();
  659. //$check_result = Result :: load (null, $user_id, $eval_id);
  660. if (empty($check_result)) {
  661. $res = new Result();
  662. $eval_data = array(
  663. 'user_id' => $user_id,
  664. 'evaluation_id' => $eval_id,
  665. //'created_at' => api_get_utc_datetime(),
  666. 'created_at' => $utc_datetime,
  667. 'score' => $data['nota'],
  668. );
  669. //$res->set_evaluation_id($eval_id);
  670. //$res->set_user_id($user_id);
  671. //if no scores are given, don't set the score
  672. //$res->set_score($data['nota']);
  673. //$res->add();
  674. $limit = 250;
  675. if (count($data_list['create_eval_results'])>$limit) {
  676. $data_list['create_eval_results'][] = $eval_data;
  677. $res->group_add($data_list['create_eval_results']);
  678. $data_list['create_eval_results'] = array();
  679. } else {
  680. $data_list['create_eval_results'][] = $eval_data;
  681. }
  682. error_log("Result saved :)");
  683. } else {
  684. error_log("Result already added ");
  685. }
  686. } else {
  687. error_log("Evaluation not found ");
  688. }
  689. } else {
  690. error_log("Gradebook does not exists");
  691. }
  692. } else {
  693. error_log("Something is wrong with the course ");
  694. }
  695. } else {
  696. error_log("NO course found for session id: $session_id");
  697. }
  698. } else {
  699. error_log("NO session id found: $session_id");
  700. }
  701. }
  702. static function add_gradebook_result_with_evaluation($data) {
  703. error_log('add_gradebook_result_with_evaluation');
  704. $session_id = isset($data['session_id']) ? $data['session_id'] : null;
  705. $user_id = isset($data['user_id']) ? $data['user_id'] : null;
  706. //Default evaluation title
  707. $title = 'Evaluación General';
  708. if (!empty($session_id) && !empty($user_id)) {
  709. global $data_list, $utc_datetime;
  710. //$course_list = SessionManager::get_course_list_by_session_id($session_id);
  711. $course_list = array(0 => array('code' => $data_list['session_course'][$session_id]));
  712. if (!empty($course_list)) {
  713. $course_data = current($course_list);
  714. if (isset($course_data['code'])) {
  715. $gradebook = array('id' => $data_list['session_course_gradebook'][$course_data['code']][$session_id]);
  716. //Get gradebook
  717. //$gradebook = new Gradebook();
  718. //$gradebook = $gradebook->get_first(array('where' => array('course_code = ? AND session_id = ?' => array($course_data['code'], $session_id))));
  719. //error_log("Looking gradebook in course code: {$course_data['code']} - session_id: $session_id, user_id: $user_id");
  720. if (!empty($gradebook)) {
  721. error_log("Gradebook exists: {$gradebook['id']}");
  722. //Creates
  723. $eval = new Evaluation();
  724. $evals_found = false;
  725. if (isset($data_list['course_evals'][$course_data['code']][$gradebook['id']][$title])) {
  726. $evals_found = $data_list['course_evals'][$course_data['code']][$gradebook['id']][$title];
  727. }
  728. if (empty($evals_found)) {
  729. $eval->set_name($title);
  730. //$eval->set_evaluation_type_id($data['gradebook_evaluation_type_id']);
  731. $eval->set_user_id(self::default_admin_id);
  732. $eval->set_course_code($course_data['code']);
  733. $eval->set_category_id($gradebook['id']);
  734. //harcoded values
  735. $eval->set_weight(100);
  736. $eval->set_max(100); //score of tinNota is over 100
  737. $eval->set_visible(1);
  738. $eval->add();
  739. $eval_id = $eval->get_id();
  740. $data_list['course_evals'][$course_data['code']][$gradebook['id']][$title] = $eval_id;
  741. } else {
  742. $eval_id = $evals_found;
  743. }
  744. if ($eval_id) {
  745. //Check if already exists
  746. //$check_result = Result :: load (null, $user_id, $eval_id);
  747. $check_result = $data_list['course_eval_results'][$eval_id][$user_id];
  748. if (empty($check_result)) {
  749. //$res = new Result();
  750. //$res->set_evaluation_id($eval_id);
  751. //$res->set_user_id($user_id);
  752. ////if no scores are given, don't set the score
  753. //$res->set_score($data['nota']);
  754. //$res_id = $res->add();
  755. $eval_data = array(
  756. 'user_id' => $user_id,
  757. 'evaluation_id' => $eval_id,
  758. //'created_at' => api_get_utc_datetime(),
  759. 'created_at' => $utc_datetime,
  760. 'score' => $data['nota'],
  761. );
  762. $data_list['course_eval_results'][$eval_id][$user_id] = $res_id;
  763. $limit = $data_list['create_eval_results_limit'];
  764. if (count($data_list['create_eval_results'])>$limit) {
  765. $data_list['create_eval_results'][] = $eval_data;
  766. $res = new Result();
  767. $res->group_add($data_list['create_eval_results']);
  768. $data_list['create_eval_results'] = array();
  769. } else {
  770. $data_list['create_eval_results'][] = $eval_data;
  771. }
  772. } else {
  773. error_log("Result already added ");
  774. }
  775. } else {
  776. error_log("Evaluation not detected");
  777. }
  778. } else {
  779. error_log("Gradebook does not exists");
  780. }
  781. } else {
  782. error_log("Something is wrong with the course ");
  783. }
  784. } else {
  785. error_log("NO course found for session id: $session_id");
  786. }
  787. } else {
  788. error_log("NO session id found: $session_id");
  789. }
  790. }
  791. /* Transaction methods */
  792. //añadir usuario: usuario_agregar UID
  793. //const TRANSACTION_TYPE_ADD_USER = 1;
  794. static function transaction_1($data, $web_service_details) {
  795. global $data_list;
  796. $uidIdPersonaId = $data['item_id'];
  797. //Add user call the webservice
  798. $user_info = Migration::soap_call($web_service_details, 'usuarioDetalles', array('intIdSede'=> $data['branch_id'], 'uididpersona' => $uidIdPersonaId));
  799. if ($user_info['error'] == false) {
  800. global $api_failureList;
  801. unset($user_info['error']);
  802. $chamilo_user_info = UserManager::add($user_info);
  803. if ($chamilo_user_info && $chamilo_user_info['user_id']) {
  804. $chamilo_user_info = api_get_user_info($chamilo_user_info['user_id'], false, false, true);
  805. $data_list['users'][$uidIdPersonaId] = $chamilo_user_info['user_id'];
  806. return array(
  807. 'entity' => 'user',
  808. 'before' => null,
  809. 'after' => $chamilo_user_info,
  810. 'message' => "User was created - user_id: {$chamilo_user_info['user_id']} - firstname: {$chamilo_user_info['firstname']} - lastname:{$chamilo_user_info['lastname']}",
  811. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  812. );
  813. } else {
  814. return array(
  815. 'message' => "User was not created : $uidIdPersonaId \n UserManager::add() params: ".print_r($user_info, 1)." \nResponse: \n ".print_r($api_failureList, 1),
  816. 'status_id' => self::TRANSACTION_STATUS_FAILED
  817. );
  818. }
  819. } else {
  820. return $user_info;
  821. }
  822. }
  823. //eliminar usuario usuario_eliminar UID
  824. //const TRANSACTION_TYPE_DEL_USER = 2;
  825. static function transaction_2($data) {
  826. $uidIdPersonaId = strtoupper($data['item_id']);
  827. global $data_list;
  828. $user_id = self::get_user_id_by_persona_id($uidIdPersonaId, $data_list);
  829. if ($user_id) {
  830. $chamilo_user_info_before = api_get_user_info($user_id, false, false, true);
  831. $result = UserManager::delete_user($user_id);
  832. $chamilo_user_info = api_get_user_info($user_id, false, false, true);
  833. if ($result) {
  834. $data_list['users'][$uidIdPersonaId] = null;
  835. return array(
  836. 'entity' => 'user',
  837. 'before' => $chamilo_user_info_before,
  838. 'after' => $chamilo_user_info,
  839. 'message' => "User was deleted : $user_id",
  840. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  841. );
  842. } else {
  843. return array(
  844. 'message' => "User was NOT deleted : $user_id error while calling function UserManager::delete_user",
  845. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  846. );
  847. }
  848. } else {
  849. return array(
  850. 'message' => "User was not found with uidIdPersona: $uidIdPersonaId",
  851. 'status_id' => self::TRANSACTION_STATUS_FAILED
  852. );
  853. }
  854. }
  855. /**
  856. * editar detalles de usuario (nombre/correo/contraseña) usuario_editar UID
  857. * const TRANSACTION_TYPE_EDIT_USER = 3;
  858. */
  859. static function transaction_3($data, $web_service_details) {
  860. $uidIdPersonaId = strtoupper($data['item_id']);
  861. global $data_list;
  862. $user_id = self::get_user_id_by_persona_id($uidIdPersonaId, $data_list);
  863. if ($user_id) {
  864. $user_info = Migration::soap_call($web_service_details, 'usuarioDetalles', array('intIdSede'=> $data['branch_id'], 'uididpersona' => $uidIdPersonaId));
  865. if ($user_info['error'] == false) {
  866. unset($user_info['error']);
  867. //Edit user
  868. $user_info['user_id'] = $user_id;
  869. $chamilo_user_info_before = api_get_user_info($user_id, false, false, true);
  870. UserManager::update($user_info);
  871. $chamilo_user_info = api_get_user_info($user_id, false, false, true);
  872. return array(
  873. 'entity' => 'user',
  874. 'before' => $chamilo_user_info_before,
  875. 'after' => $chamilo_user_info,
  876. 'message' => "User id $user_id was updated with data: ".print_r($user_info, 1),
  877. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  878. );
  879. } else {
  880. return $user_info;
  881. }
  882. } else {
  883. return array(
  884. 'message' => "User was not found with uidIdPersona: $uidIdPersonaId",
  885. 'status_id' => self::TRANSACTION_STATUS_FAILED
  886. );
  887. }
  888. }
  889. //cambiar usuario de progr. académ. (de A a B, de A a nada, de nada a A) (como estudiante o profesor) usuario_matricula UID ORIG DEST
  890. //const TRANSACTION_TYPE_SUB_USER = 4; //subscribe user to a session
  891. static function transaction_4($data) {
  892. $uidIdPersona = $data['item_id'];
  893. $uidIdPrograma = $data['orig_id'];
  894. $uidIdProgramaDestination = $data['dest_id'];
  895. $status = (!empty($data['info'])?$data['info']:null);
  896. global $data_list;
  897. $user_id = self::get_user_id_by_persona_id($uidIdPersona,$data_list);
  898. if (empty($user_id)) {
  899. return array(
  900. 'message' => "User does not exists in DB: $uidIdPersona",
  901. 'status_id' => self::TRANSACTION_STATUS_FAILED
  902. );
  903. }
  904. //Move A to B
  905. if (!empty($uidIdPrograma) && !empty($uidIdProgramaDestination)) {
  906. $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
  907. $destination_session_id = self::get_session_id_by_programa_id($uidIdProgramaDestination, $data_list);
  908. if (!empty($session_id) && !empty($destination_session_id)) {
  909. $before1 = SessionManager::get_user_status_in_session($session_id, $user_id);
  910. $before2 = SessionManager::get_user_status_in_session($destination_session_id, $user_id);
  911. /*
  912. These constants are defined in sessionmanager but are not
  913. usable directly from here
  914. SESSION_CHANGE_USER_REASON_SCHEDULE = 1;
  915. SESSION_CHANGE_USER_REASON_CLASSROOM = 2;
  916. SESSION_CHANGE_USER_REASON_LOCATION = 3;
  917. SESSION_CHANGE_USER_REASON_ENROLLMENT_ANNULATION = 4;
  918. */
  919. $reason_id = 1;
  920. SessionManager::change_user_session($user_id, $session_id, $destination_session_id, $reason_id);
  921. $befores = array($before1, $before2);
  922. $message = "Move Session A to Session B";
  923. return self::check_if_user_is_subscribe_to_session($user_id, $destination_session_id, $message, $befores);
  924. } else {
  925. return array(
  926. 'message' => "Session ids were not correctly setup session_id 1: $session_id Session id 2 $uidIdProgramaDestination - Move Session A to Session B",
  927. 'status_id' => self::TRANSACTION_STATUS_FAILED
  928. );
  929. }
  930. }
  931. //Move A to empty
  932. if (!empty($uidIdPrograma) && empty($uidIdProgramaDestination)) {
  933. $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
  934. if (!empty($session_id)) {
  935. $before = SessionManager::get_user_status_in_session($session_id, $user_id);
  936. //SessionManager::suscribe_users_to_session($session_id, array($user_id), SESSION_VISIBLE_READ_ONLY, false, false);
  937. SessionManager::unsubscribe_user_from_session($session_id, $user_id);
  938. $message = "Move Session to empty";
  939. return self::check_if_user_is_subscribe_to_session($user_id, $session_id, $message, $before);
  940. } else {
  941. return array(
  942. 'message' => "Session does not exists in DB $uidIdPrograma - Move Session to empty",
  943. 'status_id' => self::TRANSACTION_STATUS_FAILED
  944. );
  945. }
  946. }
  947. //Move empty to A
  948. if (empty($uidIdPrograma) && !empty($uidIdProgramaDestination)) {
  949. $session_id = self::get_session_id_by_programa_id($uidIdProgramaDestination, $data_list);
  950. if (!empty($session_id)) {
  951. $before = SessionManager::get_user_status_in_session($session_id, $user_id);
  952. if (isset($status) && $status == 1) {
  953. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  954. if (!empty($course_list)) {
  955. $course_data = current($course_list);
  956. SessionManager::set_coach_to_course_session($user_id, $session_id, $course_data['code']);
  957. } else {
  958. return array(
  959. 'message' => 'Could not subscribe to course: no course in session '.$uidIdProgramaDestination,
  960. 'status_id' => self::TRANSACTION_STATUS_FAILED
  961. );
  962. }
  963. } else {
  964. SessionManager::suscribe_users_to_session($session_id, array($user_id), SESSION_VISIBLE_READ_ONLY, false, false);
  965. }
  966. $message = 'Move empty to Session';
  967. return self::check_if_user_is_subscribe_to_session($user_id, $session_id, $message, $before);
  968. } else {
  969. return array(
  970. 'message' => "Session does not exists in DB $uidIdProgramaDestination - Move empty to Session",
  971. 'status_id' => self::TRANSACTION_STATUS_FAILED
  972. );
  973. }
  974. }
  975. }
  976. static function check_if_user_is_subscribe_to_session($user_id, $session_id, $message = null, $before = array()) {
  977. $user_session_status = SessionManager::get_user_status_in_session($session_id, $user_id);
  978. if (!empty($user_session_status)) {
  979. return array(
  980. 'entity' => 'session_rel_user',
  981. 'before' => $before,
  982. 'after' => $user_session_status,
  983. 'message' => "User $user_id added to Session $session_id - user relation_type in session: {$user_session_status['relation_type']}- $message ",
  984. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  985. );
  986. } else {
  987. return array(
  988. 'message' => "User $user_id was NOT added to Session $session_id - $message",
  989. 'status_id' => self::TRANSACTION_STATUS_FAILED
  990. );
  991. }
  992. }
  993. //Cursos
  994. //añadir curso curso_agregar CID
  995. //const TRANSACTION_TYPE_ADD_COURSE = 5;
  996. static function transaction_5($data, $web_service_details) {
  997. global $data_list;
  998. $uidCursoId = $data['item_id'];
  999. $course_info = Migration::soap_call($web_service_details, 'cursoDetalles', array('intIdSede'=> $data['branch_id'], 'uididcurso' => $uidCursoId));
  1000. if ($course_info['error'] == false) {
  1001. unset($course_info['error']);
  1002. $course_info = CourseManager::create_course($course_info);
  1003. $course_info = api_get_course_info($course_info['code'], true);
  1004. if (!empty($course_info)) {
  1005. $data_list['courses'][$uidCursoId] = $course_info['code'];
  1006. return array(
  1007. 'entity' => 'course',
  1008. 'before' => null,
  1009. 'after' => $course_info,
  1010. 'message' => "Course was created code: {$course_info['code']} ",
  1011. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1012. );
  1013. } else {
  1014. return array(
  1015. 'message' => "Course was NOT created",
  1016. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1017. );
  1018. }
  1019. } else {
  1020. return $course_info;
  1021. }
  1022. }
  1023. //eliminar curso curso_eliminar CID
  1024. //const TRANSACTION_TYPE_DEL_COURSE = 6;
  1025. static function transaction_6($data) {
  1026. global $data_list;
  1027. $course_code = self::get_real_course_code($data['item_id']);
  1028. if (!empty($course_code)) {
  1029. $course_info_before = api_get_course_info($course_code, true);
  1030. CourseManager::delete_course($course_code);
  1031. $course_info = api_get_course_info($course_code, true);
  1032. $data_list['courses'][$data['item_id']] = null;
  1033. return array(
  1034. 'entity' => 'course',
  1035. 'before' => $course_info_before,
  1036. 'after' => $course_info,
  1037. 'message' => "Course was deleted $course_code ",
  1038. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1039. );
  1040. } else {
  1041. return array(
  1042. 'message' => "Coursecode does not exists in DB $course_code ",
  1043. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1044. );
  1045. }
  1046. }
  1047. //editar detalles de curso curso_editar CID
  1048. //const TRANSACTION_TYPE_EDIT_COURSE = 7;
  1049. static function transaction_7($data, $web_service_details) {
  1050. $uidCursoId = $data['item_id'];
  1051. $course_code = self::get_real_course_code($uidCursoId);
  1052. if (!empty($course_code)) {
  1053. $course_info = api_get_course_info($course_code, true);
  1054. $data_to_update = Migration::soap_call($web_service_details, 'cursoDetalles', array('intIdSede'=> $data['branch_id'], 'uididcurso' => $uidCursoId));
  1055. if ($data_to_update['error'] == false) {
  1056. //do some cleaning
  1057. $data_to_update['code'] = $course_info['code'];
  1058. unset($data_to_update['error']);
  1059. CourseManager::update($data_to_update);
  1060. $course_info_after = api_get_course_info($course_code, true);
  1061. return array(
  1062. 'entity' => 'course',
  1063. 'before' => $course_info,
  1064. 'after' => $course_info_after,
  1065. 'message' => "Course with code: $course_code was updated with this data: ".print_r($data_to_update, 1),
  1066. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1067. );
  1068. } else {
  1069. return $data_to_update;
  1070. }
  1071. } else {
  1072. return array(
  1073. 'message' => "couCoursese_code does not exists $course_code ",
  1074. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1075. );
  1076. }
  1077. }
  1078. /**
  1079. * Cambiar curso de progr. académ. (de nada a A) curso_matricula CID ORIG DEST
  1080. * @todo Unused ?
  1081. *
  1082. */
  1083. static function transaction_curso_matricula($data) {
  1084. $course_code = self::get_real_course_code($data['item_id']);
  1085. $courseInfo = api_get_course_info($course_code);
  1086. $uidIdPrograma = $data['orig_id'];
  1087. $uidIdProgramaDestination = $data['dest_id'];
  1088. global $data_list;
  1089. //$session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
  1090. $destination_session_id = self::get_session_id_by_programa_id($uidIdProgramaDestination, $data_list);
  1091. if (!empty($course_code)) {
  1092. if (empty($uidIdPrograma) && !empty($uidIdProgramaDestination) && !empty($destination_session_id)) {
  1093. SessionManager::add_courses_to_session($destination_session_id, array($courseInfo['real_id']));
  1094. return array(
  1095. 'message' => "Session updated $uidIdPrograma",
  1096. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1097. );
  1098. } else {
  1099. return array(
  1100. 'message' => "Session destination was not found - [dest_id] not found",
  1101. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1102. );
  1103. }
  1104. } else {
  1105. return array(
  1106. 'message' => "Course does not exists $course_code",
  1107. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1108. );
  1109. }
  1110. }
  1111. //Programas académicos
  1112. //añadir p.a. pa_agregar PID
  1113. // const TRANSACTION_TYPE_ADD_SESS = 8;
  1114. static function transaction_8($data, $web_service_details) {
  1115. global $data_list;
  1116. $session_info = Migration::soap_call($web_service_details, 'programaDetalles', array('intIdSede'=> $data['branch_id'], 'uididprograma' => $data['item_id']));
  1117. if ($session_info['error'] == false) {
  1118. unset($session_info['error']);
  1119. // check dates (only do this at session creation)
  1120. self::fix_access_dates($session_info);
  1121. $session_id = SessionManager::add($session_info);
  1122. $session_info = api_get_session_info($session_id, true);
  1123. if ($session_id) {
  1124. $data_list['sessions'][$data['item_id']] = $session_id;
  1125. return array(
  1126. 'entity' => 'session',
  1127. 'before' => null,
  1128. 'after' => $session_info,
  1129. 'message' => "Session was created. Id: $session_id session data: ".print_r($session_info, 1),
  1130. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1131. );
  1132. } else {
  1133. return array(
  1134. 'message' => "Session was NOT created: {$data['item_id']} session data: ".print_r($session_info, 1),
  1135. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1136. );
  1137. }
  1138. } else {
  1139. //Return error
  1140. return $session_info;
  1141. }
  1142. }
  1143. //eliminar p.a. pa_eliminar PID
  1144. //const TRANSACTION_TYPE_DEL_SESS = 9;
  1145. static function transaction_9($data) {
  1146. $uidIdPrograma = $data['item_id'];
  1147. global $data_list;
  1148. $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
  1149. if (!empty($session_id)) {
  1150. $session_info_before = api_get_session_info($session_id, true);
  1151. SessionManager::delete_session($session_id, true);
  1152. $session_info = api_get_session_info($session_id, true);
  1153. $data_list['sessions'][$data['item_id']] = null;
  1154. return array(
  1155. 'entity' => 'session',
  1156. 'before' => $session_info_before,
  1157. 'after' => $session_info,
  1158. 'message' => "Session was deleted session_id: $session_id - id: $uidIdPrograma",
  1159. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1160. );
  1161. } else {
  1162. return array(
  1163. 'message' => "Session does not exists $uidIdPrograma",
  1164. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1165. );
  1166. }
  1167. }
  1168. //editar detalles de p.a. pa_editar PID
  1169. // const TRANSACTION_TYPE_EDIT_SESS = 10;
  1170. static function transaction_10($data, $web_service_details) {
  1171. $uidIdPrograma = $data['item_id'];
  1172. global $data_list;
  1173. $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
  1174. if (!empty($session_id)) {
  1175. $session_info = Migration::soap_call($web_service_details, 'programaDetalles', array('intIdSede'=> $data['branch_id'], 'uididprograma' => $data['item_id']));
  1176. if ($session_info['error'] == false) {
  1177. self::fix_access_dates($session_info);
  1178. $session_info['id'] = $session_id;
  1179. unset($session_info['error']);
  1180. $session_info_before = api_get_session_info($session_id, true);
  1181. SessionManager::update($session_info);
  1182. $session_info = api_get_session_info($session_id, true);
  1183. return array(
  1184. 'entity' => 'session',
  1185. 'before' => $session_info_before,
  1186. 'after' => $session_info,
  1187. 'message' => "Session updated $uidIdPrograma with data: ".print_r($session_info, 1),
  1188. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1189. );
  1190. } else {
  1191. return $session_info;
  1192. }
  1193. } else {
  1194. return array(
  1195. 'message' => "Session does not exists $uidIdPrograma",
  1196. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1197. );
  1198. }
  1199. }
  1200. static function transaction_cambiar_generic($extra_field_variable, $data) {
  1201. global $data_list;
  1202. $uidIdPrograma = $data['item_id'];
  1203. //$orig_id = $data['orig_id'];
  1204. $destination_id = $data['dest_id'];
  1205. $common_message = " - item_id: $uidIdPrograma, dest_id: $destination_id - looking for extra_field_variable: $extra_field_variable - with data ".print_r($data, 1);
  1206. $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
  1207. if (!empty($session_id)) {
  1208. //??
  1209. $extra_field = new ExtraField('session');
  1210. $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($extra_field_variable); //horario, aula, etc
  1211. if (empty($extra_field_info)) {
  1212. return array(
  1213. 'message' => "Extra field $extra_field_variable doest not exists in the DB $common_message",
  1214. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1215. );
  1216. }
  1217. //check if option exists
  1218. $extra_field_option = new ExtraFieldOption('session');
  1219. $extra_field_option_info = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $destination_id); //horario, aula, etc
  1220. if ($extra_field_option_info) {
  1221. $extra_field_value = new ExtraFieldValue('session');
  1222. //Getting info before
  1223. $info_before = $extra_field_value->get_values_by_handler_and_field_id($session_id, $extra_field_info['id']);
  1224. //Delete previous extra field value
  1225. $extra_field_value->delete_values_by_handler_and_field_id($session_id, $extra_field_info['id']);
  1226. $params = array(
  1227. 'session_id' => $session_id,
  1228. 'field_id' => $extra_field_info['id'],
  1229. 'field_value' => $destination_id,
  1230. );
  1231. $extra_field_value->save($params);
  1232. //Getting info after
  1233. $info_after = $extra_field_value->get_values_by_handler_and_field_id($session_id, $extra_field_info['id']);
  1234. return array(
  1235. 'entity' => $extra_field_variable,
  1236. 'before' => $info_before,
  1237. 'after' => $info_after,
  1238. 'message' => "Extra field $extra_field_variable saved with params: ".print_r($params, 1),
  1239. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1240. );
  1241. } else {
  1242. return array(
  1243. 'message' => "Option does not exists dest_id: $destination_id $common_message",
  1244. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1245. );
  1246. }
  1247. } else {
  1248. return array(
  1249. 'message' => "Session does not exists: $uidIdPrograma $common_message",
  1250. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1251. );
  1252. }
  1253. }
  1254. //cambiar aula pa_cambiar_aula PID ORIG DEST
  1255. //const TRANSACTION_TYPE_UPD_ROOM = 11;
  1256. static function transaction_11($data) {
  1257. return self::transaction_cambiar_generic('aula', $data);
  1258. }
  1259. //cambiar horario pa_cambiar_horario PID ORIG DEST
  1260. //const TRANSACTION_TYPE_UPD_SCHED = 12;
  1261. static function transaction_12($data) {
  1262. return self::transaction_cambiar_generic('horario', $data);
  1263. }
  1264. //cambiar sede pa_cambiar_sede PID ORIG DEST
  1265. //no se usa (se declara el p.a. en otra sede, nada más)
  1266. static function transaction_pa_cambiar_sede($data) {
  1267. return self::transaction_cambiar_generic('sede', $data);
  1268. }
  1269. //cambiar intensidad pa_cambiar_fase_intensidad CID ORIG DEST (id de "intensidadFase")
  1270. //no se usa (se declara el p.a. en otra sede, nada más)
  1271. static function transaction_cambiar_pa_fase($data) {
  1272. return self::transaction_cambiar_generic('fase', $data);
  1273. }
  1274. //no se usa (se declara el p.a. en otra sede, nada más)
  1275. static function transaction_cambiar_pa_intensidad($data) {
  1276. return self::transaction_cambiar_generic('intensidad', $data);
  1277. }
  1278. //-------
  1279. static function transaction_extra_field_agregar_generic($extra_field_variable, $original_data, $web_service_details, $type='session') {
  1280. $function_name = $extra_field_variable."Detalles";
  1281. $data = Migration::soap_call($web_service_details, $function_name, array('intIdSede'=> $original_data['branch_id'], "uidid".$extra_field_variable => $original_data['item_id']));
  1282. if ($data['error'] == false) {
  1283. // Exceptional treatment for specific fields
  1284. if ($extra_field_variable == 'aula') {
  1285. $data['name'] = $original_data['branch_id'].' - '.$data['name'];
  1286. }
  1287. $extra_field = new ExtraField($type);
  1288. $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($extra_field_variable);
  1289. if ($extra_field_info) {
  1290. $extra_field_option = new ExtraFieldOption($type);
  1291. $info_before = $extra_field_option->get_field_options_by_field($extra_field_info['id']);
  1292. $params = array(
  1293. 'field_id' => $extra_field_info['id'],
  1294. 'option_value' => $original_data['item_id'],
  1295. 'option_display_text' => $data['name'],
  1296. 'option_order' => null
  1297. );
  1298. $result = $extra_field_option->save_one_item($params);
  1299. $info_after = $extra_field_option->get_field_options_by_field($extra_field_info['id']);
  1300. if ($result) {
  1301. return array(
  1302. 'entity' => $extra_field_variable,
  1303. 'before' => $info_before,
  1304. 'after' => $info_after,
  1305. 'message' => "Extra field option added - $extra_field_variable was saved with data: ".print_r($params,1),
  1306. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1307. );
  1308. } else {
  1309. return array(
  1310. 'message' => "Extra field option added - $extra_field_variable was NOT saved with data: ".print_r($params,1),
  1311. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1312. );
  1313. }
  1314. } else {
  1315. return array(
  1316. 'message' => "Extra field was not found: $extra_field_variable",
  1317. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1318. );
  1319. }
  1320. } else {
  1321. return $data;
  1322. }
  1323. }
  1324. static function transaction_extra_field_editar_generic($extra_field_variable, $original_data, $web_service_details, $type='session') {
  1325. $extra_field = new ExtraField($type);
  1326. $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($extra_field_variable);
  1327. if (empty($extra_field_info)) {
  1328. return array(
  1329. 'message' => "Extra field can't be edited extra field does not exists: extra_field_variable: ".$extra_field_variable,
  1330. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1331. );
  1332. }
  1333. $extra_field_option = new ExtraFieldOption($type);
  1334. $extra_field_option_info = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $original_data['item_id']);
  1335. $function_name = $extra_field_variable."Detalles";
  1336. $params = array('intIdSede'=> $original_data['branch_id'], "uidid".$extra_field_variable => $original_data['item_id']);
  1337. $data = Migration::soap_call($web_service_details, $function_name, $params);
  1338. if ($data['error'] == false) {
  1339. // Exceptional treatment for specific fields
  1340. if ($extra_field_variable == 'aula') {
  1341. $data['name'] = $original_data['branch_id'].' - '.$data['name'];
  1342. }
  1343. //Update 1 item
  1344. if (!empty($extra_field_option_info)) {
  1345. $info_before = $extra_field_option->get_field_options_by_field($extra_field_info['id']);
  1346. if (count($extra_field_option_info) > 1) {
  1347. //var_dump($extra_field_option_info);
  1348. //Take the first one
  1349. error_log('Warning! There are several options with the same key. You should delete doubles. Check your DB with this query:');
  1350. error_log("SELECT * FROM ".$type."_field_options WHERE field_id = {$extra_field_info['id']} AND option_value = '{$original_data['item_id']}' ");
  1351. error_log('All options are going to be updated');
  1352. }
  1353. $options_updated = array();
  1354. foreach ($extra_field_option_info as $option) {
  1355. $extra_field_option_info = array(
  1356. 'id' => $option['id'],
  1357. 'field_id' => $extra_field_info['id'],
  1358. 'option_value' => $original_data['item_id'],
  1359. 'option_display_text' => $data['name'],
  1360. 'option_order' => null
  1361. );
  1362. $extra_field_option->update($extra_field_option_info);
  1363. $options_updated[] = $option['id'];
  1364. }
  1365. $info_after = $extra_field_option->get_field_options_by_field($extra_field_info['id']);
  1366. $options_updated = implode(',', $options_updated);
  1367. return array(
  1368. 'entity' => $extra_field_variable,
  1369. 'before' => $info_before,
  1370. 'after' => $info_after,
  1371. 'message' => "Extra field options id updated: $options_updated",
  1372. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1373. );
  1374. } else {
  1375. return array(
  1376. 'message' => "Extra field option not found item_id: {$original_data['item_id']}",
  1377. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1378. );
  1379. }
  1380. } else {
  1381. return $data;
  1382. }
  1383. }
  1384. /* Delete all options with option_value = item_id */
  1385. static function transaction_extra_field_eliminar_generic($extra_field_variable, $original_data, $web_service_details, $type='session') { //horario
  1386. $extra_field = new ExtraField($type);
  1387. $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($extra_field_variable);
  1388. $extra_field_option = new ExtraFieldOption($type);
  1389. $extra_field_option_info = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $original_data['item_id']);
  1390. if (!empty($extra_field_option_info)) {
  1391. $info_before = $extra_field_option->get_field_options_by_field($extra_field_info['id']);
  1392. $deleting_option_ids = array();
  1393. foreach ($extra_field_option_info as $option) {
  1394. //@todo Delete all horario in sessions?
  1395. $result = $extra_field_option->delete($option['id']);
  1396. if ($result) {
  1397. $deleting_option_ids[] = $option['id'];
  1398. }
  1399. }
  1400. $info_after = $extra_field_option->get_field_options_by_field($extra_field_info['id']);
  1401. if (!empty($deleting_option_ids)) {
  1402. $deleting_option_ids = implode(',', $deleting_option_ids);
  1403. return array(
  1404. 'entity' => $extra_field_variable,
  1405. 'before' => $info_before,
  1406. 'after' => $info_after,
  1407. 'message' => "Extra field options were deleted for the field_variable: $extra_field_variable, options id deleted: $deleting_option_ids",
  1408. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1409. );
  1410. } else {
  1411. return array(
  1412. 'message' => "Extra field option was NOT deleted. No field options ids where found for variable: '$extra_field_variable' with id:'".$original_data['item_id']."'",
  1413. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1414. );
  1415. }
  1416. } else {
  1417. return array(
  1418. 'message' => "Extra field option was NOT deleted - Extra field not found in DB. Trying to locate field_variable: '$extra_field_variable' with id: '{$original_data['item_id']}'",
  1419. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1420. );
  1421. }
  1422. }
  1423. // Horario
  1424. // añadir horario_agregar HID
  1425. // const TRANSACTION_TYPE_ADD_SCHED = 13;
  1426. static function transaction_13($data, $web_service_details) {
  1427. return self::transaction_extra_field_agregar_generic('horario', $data, $web_service_details);
  1428. }
  1429. // eliminar horario_eliminar HID
  1430. // const TRANSACTION_TYPE_DEL_SCHED = 14;
  1431. static function transaction_14($data, $web_service_details) {
  1432. return self::transaction_extra_field_eliminar_generic('horario', $data, $web_service_details);
  1433. }
  1434. // editar horario_editar HID
  1435. // const TRANSACTION_TYPE_EDIT_SCHED = 15;
  1436. static function transaction_15($data, $web_service_details) {
  1437. return self::transaction_extra_field_editar_generic('horario', $data, $web_service_details);
  1438. }
  1439. // Aula
  1440. // añadir aula_agregar AID
  1441. // const TRANSACTION_TYPE_ADD_ROOM = 16;
  1442. static function transaction_16($data, $web_service_details) {
  1443. return self::transaction_extra_field_agregar_generic('aula', $data, $web_service_details);
  1444. }
  1445. // eliminar aula_eliminar AID
  1446. // const TRANSACTION_TYPE_DEL_ROOM = 17;
  1447. static function transaction_17($data, $web_service_details) {
  1448. return self::transaction_extra_field_eliminar_generic('aula', $data, $web_service_details);
  1449. }
  1450. // editar aula_editor AID
  1451. // const TRANSACTION_TYPE_EDIT_ROOM = 18;
  1452. static function transaction_18($data, $web_service_details) {
  1453. return self::transaction_extra_field_editar_generic('aula', $data, $web_service_details);
  1454. }
  1455. // Sede
  1456. // añadir aula_agregar SID
  1457. // const TRANSACTION_TYPE_ADD_BRANCH = 19;
  1458. static function transaction_19($data, $web_service_details) {
  1459. return self::transaction_extra_field_agregar_generic('sede', $data, $web_service_details);
  1460. }
  1461. // eliminar aula_eliminar SID
  1462. // const TRANSACTION_TYPE_DEL_BRANCH = 20;
  1463. static function transaction_20($data, $web_service_details) {
  1464. return self::transaction_extra_field_eliminar_generic('sede', $data, $web_service_details);
  1465. }
  1466. // editar aula_editar SID
  1467. // const TRANSACTION_TYPE_EDIT_BRANCH = 21;
  1468. static function transaction_21($data, $web_service_details) {
  1469. return self::transaction_extra_field_editar_generic('sede', $data, $web_service_details);
  1470. }
  1471. // Frecuencia
  1472. // añadir frec FID
  1473. // const TRANSACTION_TYPE_ADD_FREQ = 22;
  1474. static function transaction_22($data, $web_service_details) {
  1475. return self::transaction_extra_field_agregar_generic('frecuencia', $data, $web_service_details, 'course');
  1476. }
  1477. // eliminar Freca_eliminar FID
  1478. // const TRANSACTION_TYPE_DEL_FREQ = 23;
  1479. static function transaction_23($data, $web_service_details) {
  1480. return self::transaction_extra_field_eliminar_generic('frecuencia', $data, $web_service_details, 'course');
  1481. }
  1482. // editar aula_editar FID
  1483. // const TRANSACTION_TYPE_EDIT_FREQ = 24;
  1484. static function transaction_24($data, $web_service_details) {
  1485. return self::transaction_extra_field_editar_generic('frecuencia', $data, $web_service_details, 'course');
  1486. }
  1487. //
  1488. // Intensidad/Fase
  1489. // añadir intfase_agregar IID
  1490. // const TRANSACTION_TYPE_ADD_INTENS = 25;
  1491. static function transaction_25($data, $web_service_details) {
  1492. return self::transaction_extra_field_agregar_generic('intensidad', $data, $web_service_details, 'course');
  1493. }
  1494. // eliminar intfase_eliminar IID
  1495. // const TRANSACTION_TYPE_DEL_INTENS = 26;
  1496. static function transaction_26($data, $web_service_details) {
  1497. return self::transaction_extra_field_eliminar_generic('intensidad', $data, $web_service_details, 'course');
  1498. }
  1499. // editar intfase_editar IID
  1500. // const TRANSACTION_TYPE_EDIT_INTENS = 27;
  1501. static function transaction_27($data, $web_service_details) {
  1502. return self::transaction_extra_field_editar_generic('intensidad', $data, $web_service_details, 'course');
  1503. }
  1504. // Fase
  1505. // añadir fase_agregar IID
  1506. // const TRANSACTION_TYPE_ADD_FASE = 28;
  1507. static function transaction_28($data, $web_service_details) {
  1508. return self::transaction_extra_field_agregar_generic('fase', $data, $web_service_details, 'course');
  1509. }
  1510. // eliminar fase_eliminar IID
  1511. // const TRANSACTION_TYPE_DEL_FASE = 29;
  1512. static function transaction_29($data, $web_service_details) {
  1513. return self::transaction_extra_field_eliminar_generic('fase', $data, $web_service_details, 'course');
  1514. }
  1515. // editar fase_editar IID
  1516. // const TRANSACTION_TYPE_EDIT_FASE = 30;
  1517. static function transaction_30($data, $web_service_details) {
  1518. return self::transaction_extra_field_editar_generic('fase', $data, $web_service_details, 'course');
  1519. }
  1520. // NOTA
  1521. // añadir nota_agregar IID
  1522. // const TRANSACTION_TYPE_ADD_NOTA = 31;
  1523. static function transaction_31($original_data, $web_service_details) {
  1524. global $data_list;
  1525. $data = Migration::soap_call($web_service_details, 'notaDetalles', array(
  1526. 'uididpersona' => $original_data['item_id'],
  1527. 'uididprograma'=> $original_data['orig_id'],
  1528. 'intIdSede' => $original_data['branch_id']));
  1529. if ($data['error'] == false) {
  1530. $uidIdPrograma = $original_data['orig_id'];
  1531. $uidIdPersona = $original_data['item_id'];
  1532. $score = $data['name'];
  1533. $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
  1534. $user_id = self::get_user_id_by_persona_id($uidIdPersona, $data_list);
  1535. if (empty($user_id)) {
  1536. return array(
  1537. 'message' => "User does not exists in DB: $uidIdPersona",
  1538. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1539. );
  1540. }
  1541. if (empty($session_id)) {
  1542. return array(
  1543. 'message' => "Session does not exists in DB: $uidIdPrograma",
  1544. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1545. );
  1546. }
  1547. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  1548. if (!empty($course_list)) {
  1549. $course_data = current($course_list);
  1550. if (isset($course_data['code'])) {
  1551. //Look for gradebook and create it if it does not exist yet
  1552. $gradebook = new Gradebook();
  1553. $gradebook = $gradebook->get_first(array('where' => array('course_code = ? AND session_id = ?' => array($course_data['code'], $session_id))));
  1554. error_log("Looking gradebook in course code: {$course_data['code']} - session_id: $session_id");
  1555. if (count($gradebook)===0) {
  1556. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  1557. $gradebook = array('id' => create_default_course_gradebook($course_data['code'], false, $session_id));
  1558. error_log('Gradebook not found. Creating one with ID '.$gradebook['id']);
  1559. $data_list['session_course_gradebook'][$course_data['code']][$session_id] = $gradebook['id'];
  1560. }
  1561. if (!empty($gradebook)) {
  1562. //Check if gradebook exists
  1563. $eval = new Evaluation();
  1564. $evals_found = $eval->load(null, null, null, $gradebook['id'], null, null);
  1565. //Try to create a gradebook evaluation
  1566. if (empty($evals_found)) {
  1567. error_log("Trying to create a new evaluation in course code: {$course_data['code']} - session_id: $session_id");
  1568. $params = array(
  1569. 'session_id' => $session_id,
  1570. 'gradebook_description' => 'Evaluación General',
  1571. 'gradebook_evaluation_type_id' => 0
  1572. );
  1573. self::create_gradebook_evaluation($params);
  1574. $evals_found = $eval->load(null, null, null, $gradebook['id'], null, null);
  1575. }
  1576. if (!empty($evals_found)) {
  1577. $evaluation = current($evals_found);
  1578. $eval_id = $evaluation->get_id();
  1579. error_log("Evaluation found in gradebook: {$gradebook['id']}, eval_id: $eval_id");
  1580. //Eval found
  1581. $res = new Result();
  1582. $check_result = Result :: load (null, $user_id, $eval_id);
  1583. if (empty($check_result)) {
  1584. $res->set_evaluation_id($eval_id);
  1585. $res->set_user_id($user_id);
  1586. //if no scores are given, don't set the score
  1587. $res->set_score($score);
  1588. $res->add();
  1589. $eval_result = Result :: load (null, $user_id, $eval_id);
  1590. return array(
  1591. 'entity' => 'gradebook_evaluation_result',
  1592. 'before' => null,
  1593. 'after' => $eval_result,
  1594. 'message' => "Gradebook result added ",
  1595. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1596. );
  1597. } else {
  1598. $message = "Result already added user_id: $user_id - eval_id: $eval_id - gradebook_id: {$gradebook['id']}";
  1599. }
  1600. } else {
  1601. $message = "Evaluation not found in gradebook: {$gradebook['id']} : in course: {$course_data['code']} - session_id: $session_id";
  1602. }
  1603. } else {
  1604. $message = "Gradebook does not exists in course: {$course_data['code']} - session_id: $session_id";
  1605. }
  1606. } else {
  1607. $message = "Something is wrong with the course ";
  1608. }
  1609. } else {
  1610. $message = "NO course found for session id: $session_id";
  1611. }
  1612. return array(
  1613. 'message' => $message,
  1614. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1615. );
  1616. } else {
  1617. return $data;
  1618. }
  1619. }
  1620. // eliminar nota_eliminar IID
  1621. // const TRANSACTION_TYPE_DEL_NOTA = 32;
  1622. static function transaction_32($original_data, $web_service_details) {
  1623. global $data_list;
  1624. $data = Migration::soap_call($web_service_details, 'notaDetalles', array(
  1625. 'uididpersona' => $original_data['item_id'],
  1626. 'uididprograma'=> $original_data['orig_id'],
  1627. 'intIdSede' => $original_data['branch_id']
  1628. ));
  1629. if ($data['error'] == false) {
  1630. $uidIdPrograma = $original_data['orig_id'];
  1631. $uidIdPersona = $original_data['item_id'];
  1632. $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
  1633. $user_id = self::get_user_id_by_persona_id($uidIdPersona, $data_list);
  1634. if (empty($user_id)) {
  1635. return array(
  1636. 'message' => "User does not exists in DB: $uidIdPersona",
  1637. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1638. );
  1639. }
  1640. if (empty($session_id)) {
  1641. return array(
  1642. 'message' => "Session does not exists in DB: $uidIdPrograma",
  1643. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1644. );
  1645. }
  1646. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  1647. if (!empty($course_list)) {
  1648. $course_data = current($course_list);
  1649. if (isset($course_data['code'])) {
  1650. $gradebook = null;
  1651. if (!empty($data_list['session_course_gradebook'][$course_data['code']][$session_id])) {
  1652. $gradebook['id'] = $data_list['session_course_gradebook'][$course_data['code']][$session_id];
  1653. } else {
  1654. $gradebook = new Gradebook();
  1655. $gradebook = $gradebook->get_first(array('where' => array('course_code = ? AND session_id = ?' => array($course_data['code'], $session_id))));
  1656. error_log("Searching for gradebook in course code: {$course_data['code']} - session_id: $session_id");
  1657. if (count($gradebook)===0) {
  1658. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  1659. $gradebook = array('id' => create_default_course_gradebook($course_data['code'], false, $session_id));
  1660. error_log('Gradebook not found. Creating one with ID '.$gradebook['id']);
  1661. $data_list['session_course_gradebook'][$course_data['code']][$session_id] = $gradebook['id'];
  1662. }
  1663. }
  1664. // At this point we tried 3 different ways of obtaining a
  1665. // gradebook id, so no chance to fail (hopefully)
  1666. if (!empty($gradebook)) {
  1667. //Check if gradebook exists
  1668. $eval = new Evaluation();
  1669. $evals_found = $eval->load(null, null, null, $gradebook['id'], null, null);
  1670. if (!empty($evals_found)) {
  1671. error_log("Gradebook exists: {$gradebook['id']}");
  1672. $evaluation = current($evals_found);
  1673. $eval_id = $evaluation->get_id();
  1674. //Eval found
  1675. $res = new Result();
  1676. $check_result = Result :: load (null, $user_id, $eval_id);
  1677. if (!empty($check_result)) {
  1678. $res->set_evaluation_id($eval_id);
  1679. $res->set_user_id($user_id);
  1680. $res->delete();
  1681. $eval_result = Result :: load (null, $user_id, $eval_id);
  1682. return array(
  1683. 'entity' => 'gradebook_evaluation_result',
  1684. 'before' => $check_result,
  1685. 'after' => $eval_result,
  1686. 'message' => "Gradebook result deleted ",
  1687. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1688. );
  1689. } else {
  1690. $message = "Gradebook result does not exist for user_id: $user_id - eval_id $eval_id - in course: {$course_data['code']} - session_id: $session_id ";
  1691. }
  1692. } else {
  1693. $message = "Evaluation not found in gradebook: {$gradebook['id']} : in course: {$course_data['code']} - session_id: $session_id";
  1694. }
  1695. } else {
  1696. $message = "Gradebook does not exists in course: {$course_data['code']} - session_id: $session_id";
  1697. }
  1698. } else {
  1699. $message = "Something is wrong with the course ";
  1700. }
  1701. } else {
  1702. $message = "NO course found for session id: $session_id";
  1703. }
  1704. return array(
  1705. 'message' => $message,
  1706. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1707. );
  1708. } else {
  1709. return $data;
  1710. }
  1711. }
  1712. // editar nota_editar IID
  1713. // const TRANSACTION_TYPE_EDIT_NOTA = 33;
  1714. static function transaction_33($original_data, $web_service_details) {
  1715. global $data_list;
  1716. $data = Migration::soap_call($web_service_details, 'notaDetalles', array(
  1717. 'uididpersona' => $original_data['item_id'],
  1718. 'uididprograma'=> $original_data['orig_id'],
  1719. 'intIdSede' => $original_data['branch_id']
  1720. ));
  1721. if ($data['error'] == false) {
  1722. $uidIdPrograma = $original_data['orig_id'];
  1723. $uidIdPersona = $original_data['item_id'];
  1724. $score = $data['name'];
  1725. $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
  1726. $user_id = self::get_user_id_by_persona_id($uidIdPersona, $data_list);
  1727. if (empty($user_id)) {
  1728. return array(
  1729. 'message' => "User does not exists in DB: $uidIdPersona",
  1730. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1731. );
  1732. }
  1733. if (empty($session_id)) {
  1734. return array(
  1735. 'message' => "Session does not exists in DB: $uidIdPrograma",
  1736. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1737. );
  1738. }
  1739. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  1740. if (!empty($course_list)) {
  1741. $course_data = current($course_list);
  1742. if (isset($course_data['code'])) {
  1743. $gradebook = new Gradebook();
  1744. $gradebook = $gradebook->get_first(array('where' => array('course_code = ? AND session_id = ?' => array($course_data['code'], $session_id))));
  1745. error_log("Looking gradebook in course code: {$course_data['code']} - session_id: $session_id");
  1746. if (!empty($gradebook)) {
  1747. //Check if gradebook exists
  1748. $eval = new Evaluation();
  1749. $evals_found = $eval->load(null, null, null, $gradebook['id'], null, null);
  1750. //Try to create a gradebook evaluation
  1751. if (empty($evals_found)) {
  1752. error_log("Trying to create a new evaluation in course code: {$course_data['code']} - session_id: $session_id");
  1753. $params = array(
  1754. 'session_id' => $session_id,
  1755. 'gradebook_description' => 'Evaluación General',
  1756. 'gradebook_evaluation_type_id' => 0
  1757. );
  1758. self::create_gradebook_evaluation($params);
  1759. $evals_found = $eval->load(null, null, null, $gradebook['id'], null, null);
  1760. }
  1761. if (!empty($evals_found)) {
  1762. $evaluation = current($evals_found);
  1763. $eval_id = $evaluation->get_id();
  1764. error_log("Gradebook exists: {$gradebook['id']} eval_id: $eval_id");
  1765. //Eval found
  1766. $res = new Result();
  1767. $check_result = Result :: load (null, $user_id, $eval_id);
  1768. if (!empty($check_result) && isset($check_result[0])) {
  1769. // Gradebook result found. Updating...
  1770. $res->set_evaluation_id($eval_id);
  1771. $res->set_user_id($user_id);
  1772. $res->set_score($score);
  1773. $res->set_id($check_result[0]->get_id());
  1774. $res->save();
  1775. $eval_result = Result :: load (null, $user_id, $eval_id);
  1776. return array(
  1777. 'entity' => 'gradebook_evaluation_result',
  1778. 'before' => $check_result,
  1779. 'after' => $eval_result,
  1780. 'message' => "Gradebook result edited ",
  1781. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1782. );
  1783. } else {
  1784. // Gradebook result not found. Creating...
  1785. // @todo disable when moving to production
  1786. $res->set_evaluation_id($eval_id);
  1787. $res->set_user_id($user_id);
  1788. //if no scores are given, don't set the score
  1789. $res->set_score($score);
  1790. $res->add();
  1791. $eval_result = Result :: load (null, $user_id, $eval_id);
  1792. //$message = "Gradebook result not modified because gradebook result does not exist for user_id: $user_id - eval_id: $eval_id - gradebook_id: {$gradebook['id']} - course: {$course_data['code']} - session_id: $session_id";
  1793. return array(
  1794. 'entity' => 'gradebook_evaluation_result',
  1795. 'before' => null,
  1796. 'after' => $eval_result,
  1797. 'message' => "Gradebook result added because it did not exist for update",
  1798. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1799. );
  1800. }
  1801. } else {
  1802. $message = "Evaluation not found in gradebook: {$gradebook['id']} : in course: {$course_data['code']} - session_id: $session_id";
  1803. }
  1804. } else {
  1805. $message = "Gradebook does not exists in course: {$course_data['code']} - session_id: $session_id";
  1806. }
  1807. } else {
  1808. $message = "Something is wrong with the course ";
  1809. }
  1810. } else {
  1811. $message = "NO course found for session id: $session_id";
  1812. }
  1813. return array(
  1814. 'message' => $message,
  1815. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1816. );
  1817. } else {
  1818. return $data;
  1819. }
  1820. }
  1821. // Asistencias
  1822. // añadir assist_agregar IID
  1823. // const TRANSACTION_TYPE_ADD_ASSIST = 34;
  1824. static function transaction_34($original_data, $web_service_details) {
  1825. global $data_list;
  1826. $data = Migration::soap_call($web_service_details, 'asistenciaDetalles', array(
  1827. 'uididpersona' => $original_data['item_id'],
  1828. 'uididprograma'=> $original_data['orig_id'],
  1829. 'uididfecha' => $original_data['info'],
  1830. 'intIdSede' => $original_data['branch_id']
  1831. ));
  1832. if ($data['error'] == false) {
  1833. $uidIdPrograma = $original_data['orig_id'];
  1834. $uidIdPersona = $original_data['item_id'];
  1835. $attendance_date = $data['date_assist'];
  1836. $attendance_user_status = $data['status']; // modified in the asistenciaDetalles function
  1837. $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
  1838. $user_id = self::get_user_id_by_persona_id($uidIdPersona, $data_list);
  1839. if (empty($user_id)) {
  1840. return array(
  1841. 'message' => "User does not exists in DB: $uidIdPersona",
  1842. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1843. );
  1844. }
  1845. if (empty($session_id)) {
  1846. return array(
  1847. 'message' => "Session does not exists $uidIdPrograma",
  1848. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1849. );
  1850. }
  1851. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  1852. if (!empty($course_list)) {
  1853. //There's only one course per session so far
  1854. $course_data = current($course_list);
  1855. if (isset($course_data['code'])) {
  1856. //Check if user exist in the session
  1857. $status = SessionManager::get_user_status_in_course_session($user_id, $course_data['id'], $session_id);
  1858. if ($status === false) {
  1859. return array(
  1860. 'message' => "User #$user_id is not registered in course code: {$course_data['code']} - session_id: $session_id",
  1861. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1862. );
  1863. }
  1864. // attendance are registered with date + time, so get time
  1865. // from session schedule
  1866. $time = self::get_horario_value($session_id);
  1867. $attendance_date .= " $time:00";
  1868. $attendance = new Attendance();
  1869. $course_info = api_get_course_info($course_data['code']);
  1870. $attendance->set_course_id($course_info['code']);
  1871. $attendance->set_course_int_id($course_info['real_id']);
  1872. $attendance->set_session_id($session_id);
  1873. $attendance_list = array();
  1874. if (is_array($data_list) && isset($data_list) && $data_list['boost_sessions'] && isset($data_list['sessions_attendances'][$course_info['real_id']][$session_id])) {
  1875. //error_log('Found reference to an existing sheet in data_list');
  1876. $list = $data_list['sessions_attendances'][$course_info['real_id']][$session_id];
  1877. foreach ($list as $at_id) {
  1878. $attendance_list[$at_id] = array('id' => $at_id);
  1879. //error_log('attendance sheet is '.$at_id);
  1880. }
  1881. } else {
  1882. $attendance_list = $attendance->get_attendances_list($course_info['real_id'], $session_id);
  1883. //error_log('Gotten attendance_sheet from table '.$attendance_list[0]['id']);
  1884. }
  1885. if (count($attendance_list)==0) {
  1886. $d = array(
  1887. 'session_id' => $session_id,
  1888. 'user_id' => $user_id,
  1889. 'fecha' => $attendance_date,
  1890. 'status' => $attendance_user_status,
  1891. );
  1892. $attendance->set_name('Asistencia');
  1893. $attendance->set_description('');
  1894. $link_to_gradebook = false;
  1895. $attendance_id = $attendance->attendance_add($link_to_gradebook, self::default_admin_id);
  1896. if (is_array($data_list) && isset($data_list) && $data_list['boost_sessions']) {
  1897. $data_list['sessions_attendances'][$course_info['real_id']][$session_id][] = $attendance_sheet_id;
  1898. }
  1899. //error_log("Attendance created in attendance_id = $attendance_id - course code: {$course_info['code']} - session_id: $session_id - $attendance_date");
  1900. //self::create_attendance($d);
  1901. /*return array(
  1902. 'entity' => 'attendance_sheet',
  1903. 'before' => null,
  1904. 'after' => $attendance_sheet_after,
  1905. 'message' => "Attendance sheet added with id: $result",
  1906. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1907. );*/
  1908. /*
  1909. return array(
  1910. 'message' => "Attendance not found for course code: {$course_info['code']} - session_id: $session_id",
  1911. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1912. );
  1913. */
  1914. //only 1 course per session
  1915. } else {
  1916. $attendance_data = current($attendance_list);
  1917. $attendance_id = $attendance_data['id'];
  1918. //error_log("Attendance found in attendance_id = $attendance_id - course code: {$course_info['code']} - session_id: $session_id - $attendance_date");
  1919. }
  1920. $cal_info = $attendance->get_attendance_calendar_data_by_date($attendance_id, $attendance_date);
  1921. if ($cal_info && isset($cal_info['id'])) {
  1922. $cal_id = $cal_info['id'];
  1923. } else {
  1924. //Creating the attendance date
  1925. $attendance->set_date_time($attendance_date);
  1926. error_log('Setting date '.$attendance_date);
  1927. $cal_id = $attendance->attendance_calendar_add($attendance_id, true);
  1928. //error_log("Creating a new calendar item $cal_id");
  1929. /*return array(
  1930. 'message' => "Attendance calendar does not exist for date: $attendance_date in attendance_id = $attendance_id - course code: {$course_info['code']} - session_id: $session_id",
  1931. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1932. );*/
  1933. }
  1934. $users_present = array($user_id => $attendance_user_status);
  1935. $attendance_sheet_info = $attendance->attendance_sheet_get_info($cal_id, $user_id);
  1936. if (empty($attendance_sheet_info)) {
  1937. $result = $attendance->attendance_sheet_add($cal_id, $users_present, $attendance_id, false, true);
  1938. $attendance_sheet_after = $attendance->attendance_sheet_get_info($cal_id, $user_id);
  1939. } else {
  1940. return array(
  1941. 'message' => "Attendance sheet can't be added, because it already exists - attendance_id: $attendance_id - cal_id: $cal_id - user_id: $user_id - course: {$course_info['code']} - session_id: $session_id ",
  1942. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1943. );
  1944. }
  1945. if ($result) {
  1946. return array(
  1947. 'entity' => 'attendance_sheet',
  1948. 'before' => null,
  1949. 'after' => $attendance_sheet_after,
  1950. 'message' => "Attendance sheet added with id: $result",
  1951. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  1952. );
  1953. } else {
  1954. return array(
  1955. 'message' => "Attendance sheet can't be added attendance_id: $attendance_id - cal_id: $cal_id - user_id: $user_id - course: {$course_info['code']} - session_id: $session_id ",
  1956. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1957. );
  1958. }
  1959. } else {
  1960. $message = "Something is wrong with the course";
  1961. }
  1962. } else {
  1963. $message = "NO course found for session id: $session_id";
  1964. }
  1965. return array(
  1966. 'message' => $message,
  1967. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1968. );
  1969. } else {
  1970. return $data;
  1971. }
  1972. }
  1973. // eliminar assist_eliminar IID
  1974. // const TRANSACTION_TYPE_DEL_ASSIST = 35;
  1975. static function transaction_35($original_data, $web_service_details) {
  1976. global $data_list;
  1977. $data = Migration::soap_call($web_service_details, 'asistenciaDetalles', array(
  1978. 'uididpersona' => $original_data['item_id'],
  1979. 'uididprograma'=> $original_data['orig_id'],
  1980. 'uididfecha' => $original_data['info'],
  1981. 'intIdSede' => $original_data['branch_id']
  1982. ));
  1983. if ($data['error'] == false) {
  1984. $uidIdPrograma = $original_data['orig_id'];
  1985. $uidIdPersona = $original_data['item_id'];
  1986. $attendance_date = $data['date_assist'];
  1987. $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
  1988. $user_id = self::get_user_id_by_persona_id($uidIdPersona, $data_list);
  1989. if (empty($user_id)) {
  1990. return array(
  1991. 'message' => "User does not exists in DB: $uidIdPersona",
  1992. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1993. );
  1994. }
  1995. if (empty($session_id)) {
  1996. return array(
  1997. 'message' => "Session does not exists $uidIdPrograma",
  1998. 'status_id' => self::TRANSACTION_STATUS_FAILED
  1999. );
  2000. }
  2001. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  2002. if (!empty($course_list)) {
  2003. $course_data = current($course_list);
  2004. if (isset($course_data['code'])) {
  2005. //Check if user exist in the session
  2006. $status = SessionManager::get_user_status_in_course_session($user_id, $course_data['id'], $session_id);
  2007. if ($status === false) {
  2008. return array(
  2009. 'message' => "User #$user_id is not registered in course code: {$course_data['code']} - session_id: $session_id",
  2010. 'status_id' => self::TRANSACTION_STATUS_FAILED
  2011. );
  2012. }
  2013. $time = self::get_horario_value($session_id);
  2014. $attendance_date .= " $time";
  2015. $attendance = new Attendance();
  2016. $course_info = api_get_course_info($course_data['code']);
  2017. $attendance->set_course_id($course_info['code']);
  2018. $attendance->set_course_int_id($course_info['real_id']);
  2019. $attendance->set_session_id($session_id);
  2020. $attendance_list = $attendance->get_attendances_list($course_info['real_id'], $session_id);
  2021. if (empty($attendance_list)) {
  2022. return array(
  2023. 'message' => "Attendance not found for course code: {$course_info['code']} - session_id: $session_id",
  2024. 'status_id' => self::TRANSACTION_STATUS_FAILED
  2025. );
  2026. //only 1 course per session
  2027. } else {
  2028. $attendance_data = current($attendance_list);
  2029. $attendance_id = $attendance_data['id'];
  2030. error_log("Attendance found in attendance_id = $attendance_id - course code: {$course_info['code']} - session_id: $session_id - $attendance_date");
  2031. }
  2032. $cal_info = $attendance->get_attendance_calendar_data_by_date($attendance_id, $attendance_date);
  2033. if ($cal_info && isset($cal_info['id'])) {
  2034. $cal_id = $cal_info['id'];
  2035. } else {
  2036. return array(
  2037. 'message' => "Attendance calendar does not exist for date: $attendance_date in attendance_id = $attendance_id - course code: {$course_info['code']} - session_id: $session_id",
  2038. 'status_id' => self::TRANSACTION_STATUS_FAILED
  2039. );
  2040. }
  2041. $attendance_sheet_before = $attendance->attendance_sheet_get_info($cal_id, $user_id);
  2042. $result = $attendance->attendance_sheet_disable($cal_id, $user_id);
  2043. $attendance_sheet_after = $attendance->attendance_sheet_get_info($cal_id, $user_id);
  2044. if ($result) {
  2045. return array(
  2046. 'entity' => 'attendance_sheet',
  2047. 'before' => $attendance_sheet_before,
  2048. 'after' => $attendance_sheet_after,
  2049. 'message' => "Attendance sheet removed",
  2050. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  2051. );
  2052. } else {
  2053. return array(
  2054. 'message' => "Attendance sheet can't be removed attendance_id: $attendance_id - cal_id: $cal_id - user_id: $user_id - course: {$course_info['code']} - session_id: $session_id ",
  2055. 'status_id' => self::TRANSACTION_STATUS_FAILED
  2056. );
  2057. }
  2058. } else {
  2059. $message = "Something is wrong with the course";
  2060. }
  2061. } else {
  2062. $message = "NO course found for session id: $session_id";
  2063. }
  2064. return array(
  2065. 'message' => $message,
  2066. 'status_id' => self::TRANSACTION_STATUS_FAILED
  2067. );
  2068. } else {
  2069. return $data;
  2070. }
  2071. }
  2072. // editar assist_editar IID
  2073. // const TRANSACTION_TYPE_EDIT_ASSIST = 36;
  2074. static function transaction_36($original_data, $web_service_details) {
  2075. global $data_list;
  2076. $data = Migration::soap_call($web_service_details, 'asistenciaDetalles', array(
  2077. 'uididpersona' => $original_data['item_id'],
  2078. 'uididprograma'=> $original_data['orig_id'],
  2079. 'uididfecha' => $original_data['info'],
  2080. 'intIdSede' => $original_data['branch_id']
  2081. ));
  2082. if ($data['error'] == false) {
  2083. $uidIdPrograma = $original_data['orig_id'];
  2084. $uidIdPersona = $original_data['item_id'];
  2085. $attendance_date = $data['date_assist'];
  2086. $attendance_user_status = $data['status']; // modified in the asistenciaDetalles function
  2087. $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
  2088. $user_id = self::get_user_id_by_persona_id($uidIdPersona, $data_list);
  2089. if (empty($user_id)) {
  2090. return array(
  2091. 'message' => "User does not exists in DB: $uidIdPersona",
  2092. 'status_id' => self::TRANSACTION_STATUS_FAILED
  2093. );
  2094. }
  2095. if (empty($session_id)) {
  2096. return array(
  2097. 'message' => "Session does not exists $uidIdPrograma",
  2098. 'status_id' => self::TRANSACTION_STATUS_FAILED
  2099. );
  2100. }
  2101. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  2102. if (!empty($course_list)) {
  2103. $course_data = current($course_list);
  2104. if (isset($course_data['code'])) {
  2105. //Check if user exist in the session
  2106. $status = SessionManager::get_user_status_in_course_session($user_id, $course_data['id'], $session_id);
  2107. if ($status === false) {
  2108. return array(
  2109. 'message' => "User #$user_id is not registered in course code: {$course_data['code']} - session_id: $session_id",
  2110. 'status_id' => self::TRANSACTION_STATUS_FAILED
  2111. );
  2112. }
  2113. $time = self::get_horario_value($session_id);
  2114. $attendance_date .= " $time:00";
  2115. $attendance = new Attendance();
  2116. $course_info = api_get_course_info($course_data['code']);
  2117. $attendance->set_course_id($course_info['code']);
  2118. $attendance->set_course_int_id($course_info['real_id']);
  2119. $attendance->set_session_id($session_id);
  2120. $attendance_list = $attendance->get_attendances_list($course_info['real_id'], $session_id);
  2121. if (empty($attendance_list)) {
  2122. return array(
  2123. 'message' => "Attendance not found for course code: {$course_info['code']} - session_id: $session_id",
  2124. 'status_id' => self::TRANSACTION_STATUS_FAILED
  2125. );
  2126. //only 1 course per session
  2127. } else {
  2128. $attendance_data = current($attendance_list);
  2129. $attendance_id = $attendance_data['id'];
  2130. error_log("Attendance found in attendance_id = $attendance_id - course code: {$course_info['code']} - session_id: $session_id - $attendance_date");
  2131. }
  2132. $cal_info = $attendance->get_attendance_calendar_data_by_date($attendance_id, $attendance_date);
  2133. if ($cal_info && isset($cal_info['id'])) {
  2134. $cal_id = $cal_info['id'];
  2135. } else {
  2136. //Creating the attendance date
  2137. $attendance->set_date_time($attendance_date);
  2138. $cal_id = $attendance->attendance_calendar_add($attendance_id, true);
  2139. error_log("Creating a new calendar item $cal_id");
  2140. /*
  2141. return array(
  2142. 'message' => "Attendance calendar does not exist for date: $attendance_date in attendance_id = $attendance_id - course code: {$course_info['code']} - session_id: $session_id",
  2143. 'status_id' => self::TRANSACTION_STATUS_FAILED
  2144. );*/
  2145. }
  2146. $users_present = array($user_id => $attendance_user_status);
  2147. $attendance_sheet_before = $attendance->attendance_sheet_get_info($cal_id, $user_id);
  2148. $result = $attendance->attendance_sheet_add($cal_id, $users_present, $attendance_id, false, true);
  2149. $attendance_sheet_after = $attendance->attendance_sheet_get_info($cal_id, $user_id);
  2150. if ($result) {
  2151. return array(
  2152. 'entity' => 'attendance_sheet',
  2153. 'before' => $attendance_sheet_before,
  2154. 'after' => $attendance_sheet_after,
  2155. 'message' => "Attendance sheet edited with",
  2156. 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL
  2157. );
  2158. } else {
  2159. return array(
  2160. 'message' => "Attendance sheet can't be edited attendance_id: $attendance_id - cal_id: $cal_id - user_id: $user_id - course: {$course_info['code']} - session_id: $session_id ",
  2161. 'status_id' => self::TRANSACTION_STATUS_FAILED
  2162. );
  2163. }
  2164. } else {
  2165. $message = "Course is not set";
  2166. }
  2167. } else {
  2168. $message = "NO course found for session id: $session_id";
  2169. }
  2170. return array(
  2171. 'message' => $message,
  2172. 'status_id' => self::TRANSACTION_STATUS_FAILED
  2173. );
  2174. } else {
  2175. return $data;
  2176. }
  2177. }
  2178. //Custom class moved here
  2179. static function transacciones($data) {
  2180. if ($data) {
  2181. $xml = $data->transaccionesResult->any;
  2182. // Cut the invalid XML and extract the valid chunk with the data
  2183. $stripped_xml = strstr($xml, '<diffgr:diffgram');
  2184. $xml = simplexml_load_string($stripped_xml);
  2185. if (!empty($xml->NewDataSet)) {
  2186. $transaction_list = array();
  2187. foreach ($xml->NewDataSet->Table as $item) { //this is a "Table" object
  2188. $item = (array) $item;
  2189. $transaction_list[] = $item;
  2190. }
  2191. return $transaction_list;
  2192. } else {
  2193. error_log('No transactions found');
  2194. }
  2195. } else {
  2196. error_log('Data is not valid');
  2197. }
  2198. }
  2199. /* object(SimpleXMLElement)#11 (5) {
  2200. ["idt"]=>
  2201. string(6) "354913"
  2202. ["idsede"]=>
  2203. string(1) "2"
  2204. ["ida"]=>
  2205. string(2) "10"
  2206. ["id"]=>
  2207. string(36) "cf0f2c9b-3e79-4960-8dec-b1a02b367921"
  2208. ["timestamp"]=>
  2209. string(12) "AAAAATbYxkg="
  2210. }
  2211. */
  2212. static function process_transactions($params, $web_service_details) {
  2213. $transactions = Migration::soap_call($web_service_details, 'transacciones', $params);
  2214. $transaction_status_list = self::get_transaction_status_list();
  2215. $counter = 0;
  2216. if (isset($transactions) && isset($transactions['error']) && $transactions['error'] == true) {
  2217. error_log($transactions['message']);
  2218. } else {
  2219. $counter = count($transactions);
  2220. error_log("Processing ".$counter." transaction(s)");
  2221. $count = 1;
  2222. $exclude_list = self::check_transactions_duplicity($transactions);
  2223. if (!empty($transactions)) {
  2224. foreach ($transactions as $id => $transaction_info) {
  2225. $result = array();
  2226. //Add transactions here
  2227. if (in_array($id,$exclude_list)) {
  2228. // Insert as deprecated
  2229. $result = self::process_transaction($transaction_info, $transaction_status_list, null, true);
  2230. } else {
  2231. // Do normal insert
  2232. $result = self::process_transaction($transaction_info, $transaction_status_list);
  2233. }
  2234. $count++;
  2235. if ($result['error'] == true) {
  2236. error_log('ERROR: '.$result['message']);
  2237. //exit;
  2238. } else {
  2239. error_log($result['message']);
  2240. }
  2241. }
  2242. }
  2243. }
  2244. return $counter;
  2245. }
  2246. static function validate_transaction($transaction_info) {
  2247. if (empty($transaction_info) || empty($transaction_info['transaction_id']) || empty($transaction_info['action']) || empty($transaction_info['branch_id']) || empty($transaction_info['item_id'])) {
  2248. return array(
  2249. 'id' => null,
  2250. 'error' => true,
  2251. 'message' => "Transaction could not be added there are some missing params: ".print_r($transaction_info, 1)
  2252. );
  2253. } else {
  2254. return true;
  2255. }
  2256. }
  2257. /**
  2258. *
  2259. * @param array simple return of the webservice transaction
  2260. * @param array
  2261. * @param bool Force deletion of the transaction if it exists already (and reexecute)
  2262. * @param int Whether this transaction should be inserted as deprecated or not
  2263. * @return int
  2264. */
  2265. static function process_transaction($transaction_info, $transaction_status_list = array(), $forced = false, $deprecated = false) {
  2266. if ($transaction_info) {
  2267. if (empty($transaction_status_list)) {
  2268. $transaction_status_list = self::get_transaction_status_list();
  2269. }
  2270. $params = array(
  2271. 'transaction_id' => isset($transaction_info['idt']) ? $transaction_info['idt'] : null,
  2272. 'action' => isset($transaction_info['ida']) ? $transaction_info['ida'] : null,
  2273. 'item_id' => isset($transaction_info['id']) ? strtoupper($transaction_info['id']) : null,
  2274. 'orig_id' => isset($transaction_info['orig']) ? $transaction_info['orig'] : null,
  2275. 'branch_id' => isset($transaction_info['idsede']) ? $transaction_info['idsede'] : null,
  2276. 'dest_id' => isset($transaction_info['dest']) ? $transaction_info['dest'] : null,
  2277. 'info' => isset($transaction_info['infoextra']) ? $transaction_info['infoextra'] : null,
  2278. 'status_id' => $deprecated ? TRANSACTION_STATUS_DEPRECATED : 0,
  2279. );
  2280. $validate = self::validate_transaction($params);
  2281. if (isset($validate['error']) && $validate['error']) {
  2282. return $validate;
  2283. }
  2284. if ($forced) {
  2285. //Delete transaction
  2286. Migration::delete_transaction_by_transaction_id($params['transaction_id'], $params['branch_id']);
  2287. }
  2288. //what to do if transaction already exists?
  2289. $transaction_info = Migration::get_transaction_by_transaction_id($params['transaction_id'], $params['branch_id']);
  2290. if (empty($transaction_info)) {
  2291. $transaction_id = Migration::add_transaction($params);
  2292. if ($deprecated) { error_log('Inserted transaction '.$transaction_id.' as deprecated'); }
  2293. if ($transaction_id) {
  2294. return array(
  2295. 'id' => $transaction_id,
  2296. 'error' => false,
  2297. 'message' => "3rd party trans id #{$params['transaction_id']} added to Chamilo, id #$transaction_id, status {$params['status_id']}"
  2298. );
  2299. } else {
  2300. return array(
  2301. 'id' => null,
  2302. 'error' => true,
  2303. 'message' => 'There was an error while creating the transaction'
  2304. );
  2305. }
  2306. } else {
  2307. //only process transaction if it was failed or to be executed or is 0 registered
  2308. if (in_array($transaction_info['status_id'], array(0, MigrationCustom::TRANSACTION_STATUS_FAILED, MigrationCustom::TRANSACTION_STATUS_TO_BE_EXECUTED))) {
  2309. return array(
  2310. 'id' => $transaction_info['id'],
  2311. 'error' => false,
  2312. 'message' => "Third party transaction id #{$params['transaction_id']} was already added to Chamilo with id #{$transaction_info['id']}. Trying to execute because transaction has status: {$transaction_status_list[$transaction_info['status_id']]['title']}"
  2313. );
  2314. } else {
  2315. return array(
  2316. 'id' => null,
  2317. 'error' => true,
  2318. 'message' => "Third party transaction id #{$params['transaction_id']} was already added to Chamilo with id #{$transaction_info['id']}. Transaction can't be executed twice. Transacion status_id = {$transaction_status_list[$transaction_info['status_id']]['title']}"
  2319. );
  2320. }
  2321. }
  2322. return array(
  2323. 'id' => null,
  2324. 'error' => true,
  2325. 'message' => 'Third party transaction was already treated'
  2326. );
  2327. } else {
  2328. return array(
  2329. 'id' => null,
  2330. 'error' => true,
  2331. 'message' => 'Third party transaction is not an array'
  2332. );
  2333. }
  2334. }
  2335. static function genericDetalles($data, $result_name, $params = array()) {
  2336. error_log("Called $result_name");// with received data ".print_r($data,1));
  2337. $original_webservice_name = $result_name;
  2338. $result_name = $result_name.'Result';
  2339. $xml = $data->$result_name->any;
  2340. // Cut the invalid XML and extract the valid chunk with the data
  2341. $stripped_xml = strstr($xml, '<diffgr:diffgram');
  2342. $xml = simplexml_load_string($stripped_xml);
  2343. if (!empty($xml->NewDataSet)) {
  2344. $item = (array)$xml->NewDataSet->Table;
  2345. $item['error'] = false;
  2346. if (isset($item['uididsede'])) {
  2347. $item['uididsede'] = strtoupper($item['uididsede']);
  2348. }
  2349. if (isset($item['uididhorario'])) {
  2350. $item['uididhorario'] = strtoupper($item['uididhorario']);
  2351. }
  2352. return $item;
  2353. } else {
  2354. return array(
  2355. 'error' => true,
  2356. 'message' => "No data when calling web service *$original_webservice_name* Result:".print_r($data, 1)." with params: ".print_r($params, 1),
  2357. 'status_id' => MigrationCustom::TRANSACTION_STATUS_FAILED,
  2358. );
  2359. }
  2360. }
  2361. /* Returns an obj with this params
  2362. object(SimpleXMLElement)#11 (7) {
  2363. ["rol"]=>
  2364. string(8) "profesor"
  2365. ["username"]=>
  2366. string(4) "3525"
  2367. ["lastname"]=>
  2368. string(15) "Zegarra Acevedo"
  2369. ["firstname"]=>
  2370. string(10) "Allen Juan"
  2371. ["phone"]=>
  2372. object(SimpleXMLElement)#13 (0) {
  2373. }
  2374. ["email"]=>
  2375. string(17) "3525@aaa.com"
  2376. ["password"]=>
  2377. string(6) "xxx"
  2378. }
  2379. **/
  2380. static function usuarioDetalles($result, $params) {
  2381. $result = self::genericDetalles($result, __FUNCTION__, $params);
  2382. if ($result['error'] == true) {
  2383. return $result;
  2384. }
  2385. $result['status'] = $result['rol'] == 'profesor' ? COURSEMANAGER : STUDENT;
  2386. $result['phone'] = (string)$result['phone'];
  2387. $result['active'] = (int)$result['active'];
  2388. $result['extra_uidIdPersona'] = strtoupper($params['uididpersona']);
  2389. unset($result['rol']);
  2390. return $result;
  2391. }
  2392. /*
  2393. ["uididsede"]=>
  2394. string(36) "7379a7d3-6dc5-42ca-9ed4-97367519f1d9"
  2395. ["uididhorario"]=>
  2396. string(36) "cdce484b-a564-4499-b587-bc32b3f82810"
  2397. ["chrperiodo"]=>
  2398. string(6) "200910"
  2399. ["display_start_date"]=>
  2400. string(25) "2009-09-30T00:00:00-05:00"
  2401. ["display_end_date"]=>
  2402. string(25) "2009-10-26T00:00:00-05:00"
  2403. ["access_start_date"]=>
  2404. string(25) "2009-09-30T00:00:00-05:00"
  2405. ["access_end_date"]=>
  2406. string(25) "2009-10-26T00:00:00-05:00"
  2407. ["course_code"]=>
  2408. string(36) "5b7e9b5a-5145-4a42-be48-223a70d9ad52"
  2409. ["id_coach"]=>
  2410. string(36) "26dbb1c1-32b7-4cf8-a81f-43d1cb231abe"
  2411. *
  2412. */
  2413. static function programaDetalles($data, $params) {
  2414. global $data_list;
  2415. $result = self::genericDetalles($data, __FUNCTION__, $params);
  2416. if ($result['error'] == true) {
  2417. return $result;
  2418. }
  2419. //Searching course code
  2420. $course_code = MigrationCustom::get_real_course_code($result['course_code']);
  2421. $result['course_code'] = $course_code;
  2422. $course_info = api_get_course_info($course_code);
  2423. //Getting sede
  2424. $extra_field = new ExtraField('session');
  2425. $extra_field_option = new ExtraFieldOption('session');
  2426. $extra_field_info = $extra_field->get_handler_field_info_by_field_variable('sede');
  2427. $extra_field_option_info_sede = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $result['uididsede']);
  2428. $sede_name = null;
  2429. if (isset($extra_field_option_info_sede[0]) && !empty($extra_field_option_info_sede[0]['option_display_text'])) {
  2430. $sede_name = $extra_field_option_info_sede[0]['option_display_text'];
  2431. }
  2432. $extra_field_info = $extra_field->get_handler_field_info_by_field_variable('aula');
  2433. $extra_field_option_info_aula = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $result['uididaula']);
  2434. $aula_name = null;
  2435. if (isset($extra_field_option_info_aula[0]) && !empty($extra_field_option_info_sede[0]['option_display_text'])) {
  2436. $aula_name = $extra_field_option_info_aula[0]['option_display_text'];
  2437. }
  2438. //Getting horario
  2439. $extra_field_info = $extra_field->get_handler_field_info_by_field_variable('horario');
  2440. $extra_field_option_info_horario = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $result['uididhorario']);
  2441. $horario_name = null;
  2442. if (isset($extra_field_option_info_horario[0]) && !empty($extra_field_option_info_horario[0]['option_display_text'])) {
  2443. $horario_name = $extra_field_option_info_horario[0]['option_display_text'];
  2444. }
  2445. //Setting the session name
  2446. $result['name'] = substr($sede_name,13).' - '.$result['chrperiodo']." - ".$course_info['title'].' '.$horario_name.' '.$aula_name;
  2447. $result['extra_uidIdPrograma'] = strtoupper($params['uididprograma']);
  2448. $result['extra_horario'] = strtoupper($result['uididhorario']);
  2449. $result['extra_sede'] = strtoupper($result['uididsede']);
  2450. $result['extra_aula'] = strtoupper($result['uididaula']);
  2451. $result['extra_periodo'] = strtoupper($result['chrperiodo']);
  2452. $result['display_start_date'] = MigrationCustom::clean_date_time_from_ws($result['display_start_date']);
  2453. $result['display_end_date'] = MigrationCustom::clean_date_time_from_ws($result['display_end_date']);
  2454. $result['access_start_date'] = MigrationCustom::clean_date_time_from_ws($result['access_start_date']);
  2455. $result['access_end_date'] = MigrationCustom::clean_date_time_from_ws($result['access_end_date']);
  2456. //$result['estado'] = intval($result['estado']);
  2457. //Searching id_coach
  2458. $result['id_coach'] = MigrationCustom::get_user_id_by_persona_id($result['id_coach'], $data_list);
  2459. unset($result['uididprograma']);
  2460. unset($result['uididsede']);
  2461. unset($result['uididhorario']);
  2462. unset($result['chrperiodo']);
  2463. return $result;
  2464. }
  2465. /**
  2466. ["name"]=>
  2467. string(42) "(A02SA) Pronunciacion Two Sabado Acelerado"
  2468. ["frecuencia"]=>
  2469. string(36) "0091cd3b-f042-11d7-b338-0050dab14015"
  2470. ["intensidad"]=>
  2471. string(36) "0091cd3d-f042-11d7-b338-0050dab14015"
  2472. ["fase"]=>
  2473. string(36) "90854fc9-f748-4e85-a4bd-ace33598417d"
  2474. ["meses"]=>
  2475. string(3) "2 "
  2476. */
  2477. static function cursoDetalles($data, $params) {
  2478. $result = self::genericDetalles($data, __FUNCTION__, $params);
  2479. if ($result['error'] == true) {
  2480. return $result;
  2481. }
  2482. $result['title'] = $result['name'];
  2483. $result['extra_frecuencia'] = strtoupper($result['frecuencia']);
  2484. $result['extra_intensidad'] = strtoupper($result['intensidad']);
  2485. $result['extra_fase'] = strtoupper($result['fase']);
  2486. $result['extra_meses'] = strtoupper($result['meses']);
  2487. $result['extra_uidIdCurso'] = strtoupper($params['uididcurso']);
  2488. unset($result['frecuencia']);
  2489. unset($result['intensidad']);
  2490. unset($result['fase']);
  2491. unset($result['name']);
  2492. unset($result['meses']);
  2493. return $result;
  2494. }
  2495. /*Calling frecuenciaDetalles
  2496. array(1) {
  2497. ["name"]=>
  2498. string(8) "Sabatino"
  2499. }*/
  2500. static function faseDetalles($data, $params) {
  2501. $result = self::genericDetalles($data, __FUNCTION__, $params);
  2502. if ($result['error'] == true) {
  2503. return $result;
  2504. }
  2505. return $result;
  2506. }
  2507. static function frecuenciaDetalles($data, $params) {
  2508. $result = self::genericDetalles($data, __FUNCTION__, $params);
  2509. if ($result['error'] == true) {
  2510. return $result;
  2511. }
  2512. return $result;
  2513. }
  2514. /*Calling intensidadDetalles
  2515. array(1) {
  2516. ["name"]=>
  2517. string(6) "Normal"
  2518. }*/
  2519. static function intensidadDetalles($data, $params) {
  2520. $result = self::genericDetalles($data, __FUNCTION__, $params);
  2521. if ($result['error'] == true) {
  2522. return $result;
  2523. }
  2524. //$result['option_value'] = $params['uididintensidad'];
  2525. return $result;
  2526. }
  2527. /*Calling mesesDetalles
  2528. Calling mesesDetalles
  2529. array(1) {
  2530. ["name"]=>
  2531. string(3) "4 "
  2532. }*/
  2533. static function mesesDetalles($data, $params) {
  2534. $result = self::genericDetalles($data, __FUNCTION__, $params);
  2535. if ($result['error'] == true) {
  2536. return $result;
  2537. }
  2538. return $result;
  2539. }
  2540. static function aulaDetalles($data, $params) {
  2541. $result = self::genericDetalles($data, __FUNCTION__, $params);
  2542. if ($result['error'] == true) {
  2543. return $result;
  2544. }
  2545. return $result;
  2546. }
  2547. /**
  2548. * Calling sedeDetalles
  2549. array(1) {
  2550. ["name"]=>
  2551. string(23) "Sede Miraflores"
  2552. }*/
  2553. static function sedeDetalles($data, $params) {
  2554. $result = self::genericDetalles($data, __FUNCTION__, $params);
  2555. if ($result['error'] == true) {
  2556. return $result;
  2557. }
  2558. return $result;
  2559. }
  2560. /*
  2561. Calling horarioDetalles
  2562. array(3) {
  2563. ["start"]=>
  2564. string(5) "08:45"
  2565. ["end"]=>
  2566. string(5) "10:15"
  2567. ["id"]=>
  2568. string(2) "62"
  2569. }*/
  2570. static function horarioDetalles($data, $params) {
  2571. $result = self::genericDetalles($data, __FUNCTION__, $params);
  2572. if ($result['error'] == true) {
  2573. return $result;
  2574. }
  2575. $result['name'] = $result['id'].' '.$result['start'].' '.$result['end'];
  2576. //$result['option_value'] = $params['uididhorario'];
  2577. unset($result['id']);
  2578. unset($result['start']);
  2579. unset($result['end']);
  2580. return $result;
  2581. }
  2582. static function notaDetalles($data, $params) {
  2583. $result = self::genericDetalles($data, __FUNCTION__, $params);
  2584. error_log(print_r($result, 1));
  2585. if ($result['error'] == true) {
  2586. return $result;
  2587. }
  2588. return $result;
  2589. }
  2590. static function asistenciaDetalles($data, $params) {
  2591. $result = self::genericDetalles($data, __FUNCTION__, $params);
  2592. error_log(print_r($result, 1));
  2593. if ($result['error'] == true) {
  2594. return $result;
  2595. }
  2596. $array = array(
  2597. 'AUS' => 0,//(ausencia)
  2598. 'TAR' => 2,//(tarde)
  2599. 'DEF' => 4, //(default -> cuando todavía no se marcó nada)
  2600. 'PRE' => 1,//(presente)
  2601. 'T45' => 3,//(tarde de más de 45 minutos)
  2602. );
  2603. if (isset($result['name'])) {
  2604. $result['name'] = trim($result['name']);
  2605. $result['status'] = isset($array[$result['name']]) ? $array[$result['name']] : 4;
  2606. }
  2607. $result['date'] = trim($result['date_assist']);
  2608. return $result;
  2609. }
  2610. /**
  2611. * Review the dates given in the session details array and make sure we
  2612. * define them in the best possible way. Although it is nice to call this
  2613. * function, in general, people from ICPNA define the date during an update
  2614. * following the creation.
  2615. * @param array Array of session data passed by reference (modified in-place)
  2616. * @return bool Always returns true
  2617. */
  2618. static function fix_access_dates(&$data) {
  2619. // Check the $data array for access_start_date, access_end_date,
  2620. // coach_access_start_date and coach_access_end_date. If any is not
  2621. // defined, reuse the period or other dates to fill it
  2622. $nt = '0000-00-00 00:00:00'; //declar a "null time"
  2623. $period = (!empty($data['extra_field_periodo'])?$data['extra_field_periodo']:'000000');
  2624. $asd = (!empty($data['access_start_date'])?$data['access_start_date']:$nt);
  2625. $aed = (!empty($data['access_end_date'])?$data['access_end_date']:$nt);
  2626. $casd = (!empty($data['coach_access_start_date'])?$data['coach_access_start_date']:$nt);
  2627. $caed = (!empty($data['coach_access_end_date'])?$data['coach_access_end_date']:$nt);
  2628. $dsd = (!empty($data['display_start_date'])?$data['display_start_date']:$asd);
  2629. $ded = (!empty($data['display_end_date'])?$data['display_end_date']:$aed);
  2630. $vstart = $vend = $nt;
  2631. $matches = array();
  2632. $match = preg_match('/-\s(\d{4})(\d{2})\s-/',$row['name'],$matches);
  2633. $now = new DateTime(null);
  2634. $cy = $now->format('Y');
  2635. $cm = $now->format('m');
  2636. if (!empty($match)) {
  2637. $ny = $y = $matches[1];
  2638. $nm = 1 + $m = $matches[2];
  2639. //ignore current month
  2640. if ($y == $cy && $m == $cm) { break; }
  2641. if ($m == 12) {
  2642. $ny = $y+1;
  2643. $nm = 1;
  2644. }
  2645. $start = new DateTime();
  2646. $end = new DateTime();
  2647. $start->setDate($y, $m, 1);
  2648. $end->setDate($ny, $nm, 1);
  2649. $end->modify('-1 day');
  2650. $vstart = $start->format('Y-m-d H:i:s');
  2651. $vend = $end->format('Y-m-d H:i:s');
  2652. }
  2653. // Now assess the situation
  2654. if ($period != '000000') {
  2655. if ($asd != $nt && $aed != $nt && $casd != $nt && $caed != $nt) {
  2656. //everything is defined, perfect, nothing to do
  2657. break;
  2658. }
  2659. if ($asd == $nt) {
  2660. //if access_start_date is undefined, re-use the period's date
  2661. $asd = $vstart;
  2662. }
  2663. if ($casd == $nt) {
  2664. //access_start_date is defined but not coach_access_start_date,
  2665. // so re-use access_start_date
  2666. $casd = $asd;
  2667. }
  2668. if ($aed == $nt) {
  2669. //if access_end_date is undefined, re-use the period's date
  2670. $aed = $vend;
  2671. }
  2672. if ($caed == $nt) {
  2673. //access_end_date is defined but not coach_access_end_date,
  2674. // so re-use access_end_date
  2675. $caed = $aed;
  2676. }
  2677. } else {
  2678. // if the period is not defined
  2679. if ($asd != $nt && $casd == $nt) {
  2680. $casd = $asd;
  2681. }
  2682. if ($aed != $nt && $caed == $nt) {
  2683. $caed = $aed;
  2684. }
  2685. }
  2686. // Fix end dates at 23:59:59 if same as start date
  2687. if ($asd == $aed) {
  2688. $aed = substr($aed,0,11).'23:59:59';
  2689. }
  2690. if ($casd == $caed) {
  2691. $caed = substr($caed,0,11).'23:59:59';
  2692. }
  2693. if ($dsd == $ded) {
  2694. $ded = substr($ded,0,11).'23:59:59';
  2695. }
  2696. $data['access_start_date'] = $asd;
  2697. $data['access_end_date'] = $aed;
  2698. $data['display_start_date'] = $dsd;
  2699. $data['display_end_date'] = $ded;
  2700. $data['coach_access_start_date'] = $casd;
  2701. $data['coach_access_end_date'] = $caed;
  2702. $data['name'] .= ' [#'.substr($dsd,8,2).']';
  2703. return true;
  2704. }
  2705. static function get_horario_value($session_id) {
  2706. $extra_field_value = new ExtraFieldValue('session');
  2707. //Getting horario info
  2708. $extra_field = new ExtraField('session');
  2709. $extra_field_info = $extra_field->get_handler_field_info_by_field_variable('horario');
  2710. $horario_info = $extra_field_value->get_values_by_handler_and_field_id($session_id, $extra_field_info['id']);
  2711. $extra_field_option = new ExtraFieldOption('session');
  2712. $horario_info = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $horario_info['field_value']);
  2713. $time = "08:00";
  2714. if (isset($horario_info) && isset($horario_info[0])) {
  2715. $horario = $horario_info[0]['option_display_text'];
  2716. $horario_array = explode(' ', $horario);
  2717. //Schedule format is "(01) 07:00 09:00" in this case. Adapt to your case
  2718. if (isset($horario_array[1])) {
  2719. $time = $horario_array[1];
  2720. }
  2721. }
  2722. return $time;
  2723. }
  2724. static function fill_data_list(&$omigrate) {
  2725. if (is_array($omigrate) && isset($omigrate) && $omigrate['boost_users']) {
  2726. // uidIdPersona field is ID 13 in user_field
  2727. $sql = "SELECT user_id, field_value FROM user_field_values WHERE field_id = 13 ORDER BY user_id";
  2728. $res = Database::query($sql);
  2729. while ($row = Database::fetch_array($res)) {
  2730. $omigrate['users'][$row['field_value']] = $row['user_id'];
  2731. }
  2732. }
  2733. if (is_array($omigrate) && isset($omigrate) && $omigrate['boost_courses']) {
  2734. // uidIdCurso field is ID 5 in course_field
  2735. $sql = "SELECT course_code, field_value FROM course_field_values WHERE field_id = 5 ORDER BY course_code";
  2736. $res = Database::query($sql);
  2737. while ($row = Database::fetch_array($res)) {
  2738. $omigrate['courses'][$row['field_value']] = $row['course_code'];
  2739. }
  2740. $sql = "SELECT id, code FROM course";
  2741. $res = Database::query($sql);
  2742. while ($row = Database::fetch_array($res)) {
  2743. $omigrate['course_ids'][$row['code']] = $row['id'];
  2744. }
  2745. }
  2746. if (is_array($omigrate) && isset($omigrate) && $omigrate['boost_sessions']) {
  2747. // uidIdPrograma field is ID 1 in session_field
  2748. $sql = "SELECT session_id, field_value FROM session_field_values WHERE field_id = 1 ORDER BY session_id";
  2749. $res = Database::query($sql);
  2750. while ($row = Database::fetch_array($res)) {
  2751. $omigrate['sessions'][$row['field_value']] = $row['session_id'];
  2752. }
  2753. $sql = "SELECT id_session, course_id, course_code FROM session_rel_course";
  2754. $res = Database::query($sql);
  2755. while ($row = Database::fetch_array($res)) {
  2756. $omigrate['session_course'][$row['id_session']] = $row['course_code'];
  2757. }
  2758. }
  2759. if (is_array($omigrate) && isset($omigrate) && $omigrate['boost_gradebooks']) {
  2760. //$evals_found = $eval->load(null, null, $course_data['code'], $gradebook['id'], null, null, $title);
  2761. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  2762. $sql = "SELECT id, course_code, category_id, name FROM $tbl_grade_evaluations ORDER BY course_code";
  2763. $res = Database::query($sql);
  2764. while ($row = Database::fetch_array($res)) {
  2765. $omigrate['course_evals'][$row['course_code']][$row['category_id']][$row['name']] = $row['id'];
  2766. }
  2767. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  2768. $sql = "SELECT id, user_id, evaluation_id FROM $tbl_grade_results ORDER BY evaluation_id";
  2769. $res = Database::query($sql);
  2770. while ($row = Database::fetch_array($res)) {
  2771. $omigrate['course_eval_results'][$row['evaluation_id']][$row['user_id']] = $row['id'];
  2772. }
  2773. $sql = "SELECT course_code, session_id, id FROM gradebook_category ORDER BY course_code, session_id";
  2774. $res = Database::query($sql);
  2775. while ($row = Database::fetch_array($res)) {
  2776. $omigrate['session_course_gradebook'][$row['course_code']][$row['session_id']] = $row['id'];
  2777. }
  2778. }
  2779. return true;
  2780. }
  2781. /**
  2782. * Check if some transactions are not deprecated because of another one
  2783. * doing the same afterwards
  2784. * @param array Array of transactions as received by the web service call
  2785. * @return array Simplified list of IDs to exclude from the transaction array
  2786. */
  2787. protected function check_transactions_duplicity($transactions) {
  2788. // The $transactions array received has rows with the following format
  2789. // $transaction_info('idt','ida','id','orig','idsede','dest','infoextra')
  2790. $cleanable = array();
  2791. // Simple repeat operations check (in particular, subscriptions cannot
  2792. // be considered cleanable)
  2793. $cleanable_actions = array(
  2794. TRANSACTION_TYPE_ADD_USER,
  2795. TRANSACTION_TYPE_DEL_USER,
  2796. TRANSACTION_TYPE_EDIT_USER,
  2797. TRANSACTION_TYPE_ADD_COURSE,
  2798. TRANSACTION_TYPE_DEL_COURSE,
  2799. TRANSACTION_TYPE_EDIT_COURSE,
  2800. TRANSACTION_TYPE_ADD_SESS,
  2801. TRANSACTION_TYPE_DEL_SESS,
  2802. TRANSACTION_TYPE_EDIT_SESS,
  2803. TRANSACTION_TYPE_ADD_SCHED,
  2804. TRANSACTION_TYPE_DEL_SCHED,
  2805. TRANSACTION_TYPE_ADD_ROOM,
  2806. TRANSACTION_TYPE_DEL_ROOM,
  2807. TRANSACTION_TYPE_EDIT_ROOM,
  2808. TRANSACTION_TYPE_ADD_BRANCH,
  2809. TRANSACTION_TYPE_DEL_BRANCH,
  2810. TRANSACTION_TYPE_EDIT_BRANCH,
  2811. TRANSACTION_TYPE_ADD_FREQ,
  2812. TRANSACTION_TYPE_DEL_FREQ,
  2813. TRANSACTION_TYPE_EDIT_FREQ,
  2814. TRANSACTION_TYPE_ADD_INTENS,
  2815. TRANSACTION_TYPE_DEL_INTENS,
  2816. TRANSACTION_TYPE_EDIT_INTENS,
  2817. TRANSACTION_TYPE_ADD_FASE,
  2818. TRANSACTION_TYPE_DEL_FASE,
  2819. TRANSACTION_TYPE_EDIT_FASE,
  2820. );
  2821. $list_all = array_keys($transactions);
  2822. $exclude = array();
  2823. // Browse through all transactions
  2824. foreach ($transactions as $id => $t) {
  2825. //If the item is in the "cleanable actions" list, register it.
  2826. // Otherwise, just ignore (it won't be inserted in the excluded
  2827. // list)
  2828. if (in_array($t['ida'],$cleanable_actions)) {
  2829. if (isset($cleanable[$t['id']][$t['ida']])) {
  2830. //if same id, same action was already defined, remember id
  2831. // for exclusion
  2832. error_log('Excluding duplicated transaction '.$t['idt'].'. Transaction '.$transactions[$cleanable[$t['id']][$t['ida']]]['idt'].' duplicates it');
  2833. error_log('SELECT * FROM branch_transaction WHERE branch_id = '.$t['idsede'].' AND transaction_id IN ('.$t['idt'].','.$transactions[$cleanable[$t['id']][$t['ida']]]['idt'].');');
  2834. $exclude[] = $cleanable[$t['id']][$t['ida']];
  2835. }
  2836. $cleanable[$t['id']][$t['ida']] = $id;
  2837. }
  2838. }
  2839. return $exclude;
  2840. }
  2841. }