123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126 |
- <?php
- /* For licensing terms, see /license.txt */
- use Chamilo\CourseBundle\Entity\CCalendarEvent;
- use Chamilo\CourseBundle\Entity\CItemProperty;
- use Chamilo\PluginBundle\Entity\StudentFollowUp\CarePost;
- use Fhaculty\Graph\Graph;
- use Monolog\Handler\BufferHandler;
- use Monolog\Handler\RotatingFileHandler;
- use Monolog\Handler\StreamHandler;
- use Monolog\Logger;
- if (PHP_SAPI != 'cli') {
- die('Run this script through the command line or comment this line in the code');
- }
- if (file_exists('multiple_url_fix.php')) {
- require 'multiple_url_fix.php';
- }
- require_once __DIR__.'/../inc/global.inc.php';
- ini_set('memory_limit', -1);
- ini_set('max_execution_time', 0);
- ini_set('log_errors', '1');
- ini_set('display_errors', '1');
- /**
- * Class ImportCsv.
- */
- class ImportCsv
- {
- public $test;
- public $defaultLanguage = 'dutch';
- public $extraFieldIdNameList = [
- 'session' => 'external_session_id',
- 'session_career' => 'external_career_id',
- 'course' => 'external_course_id',
- 'user' => 'external_user_id',
- 'calendar_event' => 'external_calendar_event_id',
- 'career' => 'external_career_id',
- 'career_urls' => 'career_urls',
- 'career_diagram' => 'career_diagram',
- ];
- public $defaultAdminId = 1;
- public $defaultSessionVisibility = 1;
- /**
- * When creating a user the expiration date is set to registration date + this value.
- *
- * @var int number of years
- */
- public $expirationDateInUserCreation = 1;
- public $batchSize = 20;
- /**
- * When updating a user the expiration date is set to update date + this value.
- *
- * @var int number of years
- */
- public $expirationDateInUserUpdate = 1;
- public $daysCoachAccessBeforeBeginning = 14;
- public $daysCoachAccessAfterBeginning = 14;
- public $conditions;
- private $logger;
- private $dumpValues;
- private $updateEmailToDummy;
- /**
- * @param Monolog\Logger $logger
- * @param array
- */
- public function __construct($logger, $conditions)
- {
- $this->logger = $logger;
- $this->conditions = $conditions;
- $this->updateEmailToDummy = false;
- }
- /**
- * @param bool $dump
- */
- public function setDumpValues($dump)
- {
- $this->dumpValues = $dump;
- }
- /**
- * @return bool
- */
- public function getDumpValues()
- {
- return $this->dumpValues;
- }
- /**
- * Runs the import process.
- */
- public function run()
- {
- global $_configuration;
- $value = api_get_configuration_value('import_csv_custom_url_id');
- if (!empty($value)) {
- $_configuration['access_url'] = $value;
- }
- $path = api_get_path(SYS_CODE_PATH).'cron/incoming/';
- if (!is_dir($path)) {
- echo "The folder! $path does not exits";
- return 0;
- }
- if ($this->getDumpValues()) {
- $this->dumpDatabaseTables();
- }
- echo 'Reading files: '.PHP_EOL.PHP_EOL;
- $files = scandir($path);
- $fileToProcess = [];
- $fileToProcessStatic = [];
- $teacherBackup = [];
- $groupBackup = [];
- $this->prepareImport();
- if (!empty($files)) {
- foreach ($files as $file) {
- $fileInfo = pathinfo($file);
- if (isset($fileInfo['extension']) && $fileInfo['extension'] === 'csv') {
- // Checking teachers_yyyymmdd.csv,
- // courses_yyyymmdd.csv, students_yyyymmdd.csv and sessions_yyyymmdd.csv
- $parts = explode('_', $fileInfo['filename']);
- $preMethod = ucwords($parts[1]);
- $preMethod = str_replace('-static', 'Static', $preMethod);
- $method = 'import'.$preMethod;
- $isStatic = strpos($method, 'Static');
- if ($method == 'importSessionsextidStatic') {
- $method = 'importSessionsExtIdStatic';
- }
- if ($method == 'importCourseinsertStatic') {
- $method = 'importSubscribeUserToCourse';
- }
- if ($method == 'importUnsubsessionsextidStatic') {
- $method = 'importUnsubsessionsExtidStatic';
- }
- if ($method == 'importCareersdiagram') {
- $method = 'importCareersDiagram';
- }
- if ($method == 'importOpensessions') {
- $method = 'importOpenSessions';
- }
- if ($method == 'importSubsessionsextidStatic') {
- $method = 'importSubscribeUserToCourseSessionExtStatic';
- }
- if (method_exists($this, $method)) {
- if ((
- $method == 'importSubscribeStatic' ||
- $method == 'importSubscribeUserToCourse'
- ) ||
- empty($isStatic)
- ) {
- $fileToProcess[$parts[1]][] = [
- 'method' => $method,
- 'file' => $path.$fileInfo['basename'],
- ];
- } else {
- $fileToProcessStatic[$parts[1]][] = [
- 'method' => $method,
- 'file' => $path.$fileInfo['basename'],
- ];
- }
- } else {
- echo "Error - This file '$file' can't be processed.".PHP_EOL;
- echo "Trying to call $method".PHP_EOL;
- echo "The file have to has this format:".PHP_EOL;
- echo "prefix_students_ddmmyyyy.csv, prefix_teachers_ddmmyyyy.csv,
- prefix_courses_ddmmyyyy.csv, prefix_sessions_ddmmyyyy.csv ".PHP_EOL;
- exit;
- }
- }
- }
- if (empty($fileToProcess) && empty($fileToProcessStatic)) {
- echo 'Error - no files to process.';
- return 0;
- }
- $sections = [
- 'students',
- 'teachers',
- 'courses',
- 'sessions',
- 'opensessions',
- 'subscribe-static',
- 'courseinsert-static',
- 'unsubscribe-static',
- 'care',
- 'careers',
- 'careersdiagram',
- ];
- foreach ($sections as $section) {
- if (isset($fileToProcess[$section]) && !empty($fileToProcess[$section])) {
- $this->logger->addInfo("-- Import $section --");
- $files = $fileToProcess[$section];
- foreach ($files as $fileInfo) {
- $method = $fileInfo['method'];
- $file = $fileInfo['file'];
- echo 'File: '.$file.PHP_EOL;
- echo 'Method : '.$method.PHP_EOL;
- echo PHP_EOL;
- $this->logger->addInfo('====================================================');
- $this->logger->addInfo("Reading file: $file");
- $this->logger->addInfo("Loading method $method ");
- if ($method == 'importSessions' || $method == 'importOpenSessions') {
- $this->$method(
- $file,
- true,
- $teacherBackup,
- $groupBackup
- );
- } else {
- $this->$method($file, true);
- }
- $this->logger->addInfo('--Finish reading file--');
- }
- }
- }
- $sections = [
- 'students-static',
- 'teachers-static',
- 'courses-static',
- 'sessions-static',
- 'sessionsextid-static',
- 'unsubscribe-static',
- 'unsubsessionsextid-static',
- 'subsessionsextid-static',
- 'calendar-static',
- ];
- foreach ($sections as $section) {
- if (isset($fileToProcessStatic[$section]) &&
- !empty($fileToProcessStatic[$section])
- ) {
- $this->logger->addInfo("-- Import static files $section --");
- $files = $fileToProcessStatic[$section];
- foreach ($files as $fileInfo) {
- $method = $fileInfo['method'];
- $file = $fileInfo['file'];
- echo 'Static file: '.$file.PHP_EOL;
- echo 'Method : '.$method.PHP_EOL;
- echo PHP_EOL;
- $this->logger->addInfo("Reading static file: $file");
- $this->logger->addInfo("Loading method $method ");
- $this->$method(
- $file,
- true,
- $teacherBackup,
- $groupBackup
- );
- $this->logger->addInfo('--Finish reading file--');
- }
- }
- }
- $this->logger->addInfo('teacher backup');
- $this->logger->addInfo(print_r($teacherBackup, 1));
- }
- }
- /**
- * @param $file
- * @param bool $moveFile
- */
- public function importCare($file, $moveFile = false)
- {
- $data = Import::csv_reader($file);
- $counter = 1;
- $batchSize = $this->batchSize;
- $em = Database::getManager();
- if (!empty($data)) {
- $this->logger->addInfo(count($data)." records found.");
- $items = [];
- foreach ($data as $list) {
- $post = [];
- foreach ($list as $key => $value) {
- $key = (string) trim($key);
- // Remove utf8 bom
- $key = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $key);
- $post[$key] = $value;
- }
- if (empty($post)) {
- continue;
- }
- $externalId = $post['External_care_id'];
- $items[$externalId] = $post;
- }
- ksort($items);
- foreach ($items as $row) {
- // Insert user
- //$insertUserInfo = api_get_user_info_from_username($row['Added_by']);
- // User about the post
- $userId = UserManager::get_user_id_from_original_id(
- $row['Added_by'],
- $this->extraFieldIdNameList['user']
- );
- $insertUserInfo = api_get_user_info($userId);
- if (empty($insertUserInfo)) {
- $this->logger->addInfo("User: '".$row['Added_by']."' doesn't exists. Skip this entry.");
- continue;
- }
- $insertUserInfo = api_get_user_entity($insertUserInfo['user_id']);
- // User about the post
- $userId = UserManager::get_user_id_from_original_id(
- $row['External_user_id'],
- $this->extraFieldIdNameList['user']
- );
- if (empty($userId)) {
- $this->logger->addInfo("User does '".$row['External_user_id']."' not exists skip this entry.");
- continue;
- }
- $userInfo = api_get_user_entity($userId);
- if (empty($userInfo)) {
- $this->logger->addInfo("Chamilo user does not found: #".$userId."' ");
- continue;
- }
- // Dates
- $createdAt = $this->createDateTime($row['Added_On']);
- $updatedAt = $this->createDateTime($row['Edited_on']);
- // Parent
- $parent = null;
- if (!empty($row['Parent_id'])) {
- $parentId = $items[$row['Parent_id']];
- $criteria = [
- 'externalCareId' => $parentId,
- ];
- $parent = $em->getRepository('ChamiloPluginBundle:StudentFollowUp\CarePost')->findOneBy($criteria);
- }
- // Tags
- $tags = explode(',', $row['Tags']);
- // Check if post already was added:
- $criteria = [
- 'externalCareId' => $row['External_care_id'],
- ];
- $post = $em->getRepository('ChamiloPluginBundle:StudentFollowUp\CarePost')->findOneBy($criteria);
- if (empty($post)) {
- $post = new CarePost();
- $this->logger->addInfo("New post will be created no match for externalCareId = ".$row['External_care_id']);
- }
- $contentDecoded = utf8_encode(base64_decode($row['Article']));
- $post
- ->setTitle($row['Title'])
- ->setContent($contentDecoded)
- ->setExternalCareId($row['External_care_id'])
- ->setCreatedAt($createdAt)
- ->setUpdatedAt($updatedAt)
- ->setPrivate((int) $row['Private'])
- ->setInsertUser($insertUserInfo)
- ->setExternalSource((int) $row['Source_is_external'])
- ->setParent($parent)
- ->setTags($tags)
- ->setUser($userInfo)
- ->setAttachment($row['Attachement'])
- ;
- $em->persist($post);
- $em->flush();
- $this->logger->addInfo("Post id saved #".$post->getId());
- if (($counter % $batchSize) === 0) {
- $em->flush();
- $em->clear(); // Detaches all objects from Doctrine!
- }
- $counter++;
- }
- $em->clear(); // Detaches all objects from Doctrine!
- }
- }
- /**
- * @return mixed
- */
- public function getUpdateEmailToDummy()
- {
- return $this->updateEmailToDummy;
- }
- /**
- * @param mixed $updateEmailToDummy
- */
- public function setUpdateEmailToDummy($updateEmailToDummy)
- {
- $this->updateEmailToDummy = $updateEmailToDummy;
- }
- /**
- * Change emails of all users except admins.
- */
- public function updateUsersEmails()
- {
- if ($this->getUpdateEmailToDummy() === true) {
- $sql = "UPDATE user SET email = CONCAT(username,'@example.com') WHERE id NOT IN (SELECT user_id FROM admin)";
- Database::query($sql);
- }
- }
- /**
- * Prepares extra fields before the import.
- */
- private function prepareImport()
- {
- // Create user extra field: extra_external_user_id
- UserManager::create_extra_field(
- $this->extraFieldIdNameList['user'],
- 1,
- 'External user id',
- null
- );
- // Create course extra field: extra_external_course_id
- CourseManager::create_course_extra_field(
- $this->extraFieldIdNameList['course'],
- 1,
- 'External course id',
- ''
- );
- CourseManager::create_course_extra_field(
- 'disable_import_calendar',
- 13,
- 'Disable import calendar',
- ''
- );
- // Create session extra field extra_external_session_id
- SessionManager::create_session_extra_field(
- $this->extraFieldIdNameList['session'],
- 1,
- 'External session id'
- );
- SessionManager::create_session_extra_field(
- $this->extraFieldIdNameList['session_career'],
- 1,
- 'Career id'
- );
- // Create calendar_event extra field extra_external_session_id
- $extraField = new ExtraField('calendar_event');
- $extraField->save(
- [
- 'field_type' => ExtraField::FIELD_TYPE_TEXT,
- 'variable' => $this->extraFieldIdNameList['calendar_event'],
- 'display_text' => 'External calendar event id',
- ]
- );
- $extraField = new ExtraField('career');
- $extraField->save(
- [
- 'visible_to_self' => 1,
- 'field_type' => ExtraField::FIELD_TYPE_TEXT,
- 'variable' => $this->extraFieldIdNameList['career'],
- 'display_text' => 'External career id',
- ]
- );
- $extraField->save(
- [
- 'visible_to_self' => 1,
- 'field_type' => ExtraField::FIELD_TYPE_TEXTAREA,
- 'variable' => $this->extraFieldIdNameList['career_diagram'],
- 'display_text' => 'Career diagram',
- ]
- );
- $extraField->save(
- [
- 'visible_to_self' => 1,
- 'field_type' => ExtraField::FIELD_TYPE_TEXTAREA,
- 'variable' => $this->extraFieldIdNameList['career_urls'],
- 'display_text' => 'Career urls',
- ]
- );
- }
- /**
- * @param string $file
- */
- private function moveFile($file)
- {
- $moved = str_replace('incoming', 'treated', $file);
- if ($this->test) {
- $result = 1;
- } else {
- $result = rename($file, $moved);
- }
- if ($result) {
- $this->logger->addInfo("Moving file to the treated folder: $file");
- } else {
- $this->logger->addError(
- "Error - Cant move file to the treated folder: $file"
- );
- }
- }
- /**
- * @param array $row
- *
- * @return array
- */
- private function cleanUserRow($row)
- {
- $row['lastname'] = $row['LastName'];
- $row['firstname'] = $row['FirstName'];
- $row['email'] = $row['Email'];
- $row['username'] = $row['UserName'];
- $row['password'] = $row['Password'];
- $row['auth_source'] = isset($row['AuthSource']) ? $row['AuthSource'] : PLATFORM_AUTH_SOURCE;
- $row['official_code'] = $row['OfficialCode'];
- $row['phone'] = isset($row['PhoneNumber']) ? $row['PhoneNumber'] : '';
- if (isset($row['StudentID'])) {
- $row['extra_'.$this->extraFieldIdNameList['user']] = $row['StudentID'];
- }
- if (isset($row['TeacherID'])) {
- $row['extra_'.$this->extraFieldIdNameList['user']] = $row['TeacherID'];
- }
- return $row;
- }
- /**
- * @param array $row
- *
- * @return array
- */
- private function cleanCourseRow($row)
- {
- $row['title'] = $row['Title'];
- $row['course_code'] = $row['Code'];
- $row['course_category'] = $row['CourseCategory'];
- $row['email'] = $row['Teacher'];
- $row['language'] = $row['Language'];
- $row['visibility'] = isset($row['Visibility']) ? $row['Visibility'] : COURSE_VISIBILITY_REGISTERED;
- $row['teachers'] = [];
- if (isset($row['Teacher']) && !empty($row['Teacher'])) {
- $this->logger->addInfo("Teacher list found: ".$row['Teacher']);
- $teachers = explode(',', $row['Teacher']);
- if (!empty($teachers)) {
- foreach ($teachers as $teacherUserName) {
- $teacherUserName = trim($teacherUserName);
- $userInfo = api_get_user_info_from_username($teacherUserName);
- if (!empty($userInfo)) {
- $this->logger->addInfo("Username found: $teacherUserName");
- $row['teachers'][] = $userInfo['user_id'];
- }
- }
- }
- }
- if (isset($row['CourseID'])) {
- $row['extra_'.$this->extraFieldIdNameList['course']] = $row['CourseID'];
- }
- return $row;
- }
- /**
- * File to import.
- *
- * @param string $file
- */
- private function importTeachersStatic($file)
- {
- $this->importTeachers($file, true);
- }
- /**
- * File to import.
- *
- * @param string $file
- * @param bool $moveFile
- */
- private function importTeachers($file, $moveFile = true)
- {
- $this->fixCSVFile($file);
- $data = Import::csvToArray($file);
- /* Unique identifier: official-code username.
- Email address and password should never get updated. *ok
- The only fields that I can think of that should update if the data changes in the csv file are FirstName and LastName. *ok
- A slight edit of these fields should be taken into account. ???
- Adding teachers is no problem, but deleting them shouldn’t be automated, but we should get a log of “to delete teachers”.
- We’ll handle that manually if applicable.
- No delete!
- */
- $language = $this->defaultLanguage;
- if (!empty($data)) {
- $this->logger->addInfo(count($data)." records found.");
- $expirationDateOnCreation = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserCreation)."years"));
- $expirationDateOnUpdate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserUpdate)."years"));
- $batchSize = $this->batchSize;
- $em = Database::getManager();
- $counter = 1;
- foreach ($data as $row) {
- $row = $this->cleanUserRow($row);
- $user_id = UserManager::get_user_id_from_original_id(
- $row['extra_'.$this->extraFieldIdNameList['user']],
- $this->extraFieldIdNameList['user']
- );
- $userInfo = [];
- $userInfoByOfficialCode = null;
- if (!empty($user_id)) {
- $userInfo = api_get_user_info($user_id);
- $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
- }
- if (empty($userInfo) && empty($userInfoByOfficialCode)) {
- // Create user
- $userId = UserManager::create_user(
- $row['firstname'],
- $row['lastname'],
- COURSEMANAGER,
- $row['email'],
- $row['username'],
- $row['password'],
- $row['official_code'],
- $language, //$row['language'],
- $row['phone'],
- null, //$row['picture'], //picture
- $row['auth_source'], // ?
- $expirationDateOnCreation, //'0000-00-00 00:00:00', //$row['expiration_date'], //$expiration_date = '0000-00-00 00:00:00',
- 1, //active
- 0,
- null, // extra
- null, //$encrypt_method = '',
- false //$send_mail = false
- );
- $row['extra_mail_notify_invitation'] = 1;
- $row['extra_mail_notify_message'] = 1;
- $row['extra_mail_notify_group_message'] = 1;
- if ($userId) {
- foreach ($row as $key => $value) {
- if (substr($key, 0, 6) == 'extra_') {
- //an extra field
- UserManager::update_extra_field_value(
- $userId,
- substr($key, 6),
- $value
- );
- }
- }
- $this->logger->addInfo("Teachers - User created: ".$row['username']);
- } else {
- $this->logger->addError("Teachers - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
- $this->logger->addError(strip_tags(Display::getFlashToString()));
- Display::cleanFlashMessages();
- }
- } else {
- if (empty($userInfo)) {
- $this->logger->addError("Teachers - Can't update user :".$row['username']);
- continue;
- }
- // Update user
- $result = UserManager::update_user(
- $userInfo['user_id'],
- $row['firstname'], // <<-- changed
- $row['lastname'], // <<-- changed
- $userInfo['username'],
- null, //$password = null,
- $row['auth_source'],
- $userInfo['email'],
- COURSEMANAGER,
- $userInfo['official_code'],
- $userInfo['phone'],
- $userInfo['picture_uri'],
- $expirationDateOnUpdate,
- $userInfo['active'],
- null, //$creator_id = null,
- 0, //$hr_dept_id = 0,
- null, // $extra = null,
- null, //$language = 'english',
- null, //$encrypt_method = '',
- false, //$send_email = false,
- 0 //$reset_password = 0
- );
- if ($result) {
- foreach ($row as $key => $value) {
- if (substr($key, 0, 6) == 'extra_') {
- //an extra field
- UserManager::update_extra_field_value(
- $userInfo['user_id'],
- substr($key, 6),
- $value
- );
- }
- }
- $this->logger->addInfo("Teachers - User updated: ".$row['username']);
- } else {
- $this->logger->addError("Teachers - User not updated: ".$row['username']);
- }
- }
- if (($counter % $batchSize) === 0) {
- $em->flush();
- $em->clear(); // Detaches all objects from Doctrine!
- }
- $counter++;
- }
- $em->clear(); // Detaches all objects from Doctrine!
- }
- if ($moveFile) {
- $this->moveFile($file);
- }
- $this->updateUsersEmails();
- }
- /**
- * @param string $file
- */
- private function importStudentsStatic($file)
- {
- $this->importStudents($file, true);
- }
- /**
- * @param string $file
- * @param bool $moveFile
- */
- private function importStudents($file, $moveFile = true)
- {
- $this->fixCSVFile($file);
- $data = Import::csvToArray($file);
- /*
- * Another users import.
- Unique identifier: official code and username . ok
- Password should never get updated. ok
- If an update should need to occur (because it changed in the .csv),
- we’ll want that logged. We will handle this manually in that case.
- All other fields should be updateable, though passwords should of course not get updated. ok
- If a user gets deleted (not there anymore),
- He should be set inactive one year after the current date.
- So I presume you’ll just update the expiration date.
- We want to grant access to courses up to a year after deletion.
- */
- $timeStart = microtime(true);
- $batchSize = $this->batchSize;
- $em = Database::getManager();
- if (!empty($data)) {
- $language = $this->defaultLanguage;
- $this->logger->addInfo(count($data)." records found.");
- $expirationDateOnCreate = api_get_utc_datetime(
- strtotime("+".intval($this->expirationDateInUserCreation)."years")
- );
- $expirationDateOnUpdate = api_get_utc_datetime(
- strtotime("+".intval($this->expirationDateInUserUpdate)."years")
- );
- $counter = 1;
- $secondsInYear = 365 * 24 * 60 * 60;
- foreach ($data as $row) {
- $row = $this->cleanUserRow($row);
- $user_id = UserManager::get_user_id_from_original_id(
- $row['extra_'.$this->extraFieldIdNameList['user']],
- $this->extraFieldIdNameList['user']
- );
- $userInfo = [];
- $userInfoByOfficialCode = null;
- if (!empty($user_id)) {
- $userInfo = api_get_user_info($user_id, false, true);
- $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
- }
- $userInfoFromUsername = api_get_user_info_from_username($row['username']);
- if (!empty($userInfoFromUsername)) {
- $extraFieldValue = new ExtraFieldValue('user');
- $extraFieldValues = $extraFieldValue->get_values_by_handler_and_field_variable(
- $userInfoFromUsername['user_id'],
- $this->extraFieldIdNameList['user']
- );
- if (!empty($extraFieldValues)) {
- $value = 0;
- foreach ($extraFieldValues as $extraFieldValue) {
- $value = $extraFieldValue['value'];
- }
- if (!empty($user_id) && $value != $user_id) {
- $emails = api_get_configuration_value('cron_notification_help_desk');
- if (!empty($emails)) {
- $this->logger->addInfo('Preparing email to users in configuration: "cron_notification_help_desk"');
- $subject = 'User not added due to same username';
- $body = 'Cannot add username: "'.$row['username'].'"
- with external_user_id: '.$row['extra_'.$this->extraFieldIdNameList['user']].'
- because '.$userInfoFromUsername['username'].' with external_user_id '.$value.' exists on the portal';
- $this->logger->addInfo($body);
- foreach ($emails as $email) {
- api_mail_html('', $email, $subject, $body);
- }
- }
- }
- }
- }
- if (empty($userInfo) && empty($userInfoByOfficialCode)) {
- // Create user
- $result = UserManager::create_user(
- $row['firstname'],
- $row['lastname'],
- STUDENT,
- $row['email'],
- $row['username'],
- $row['password'],
- $row['official_code'],
- $language, //$row['language'],
- $row['phone'],
- null, //$row['picture'], //picture
- $row['auth_source'], // ?
- $expirationDateOnCreate,
- 1, //active
- 0,
- null, // extra
- null, //$encrypt_method = '',
- false //$send_mail = false
- );
- $row['extra_mail_notify_invitation'] = 1;
- $row['extra_mail_notify_message'] = 1;
- $row['extra_mail_notify_group_message'] = 1;
- if ($result) {
- foreach ($row as $key => $value) {
- if (substr($key, 0, 6) === 'extra_') {
- //an extra field
- UserManager::update_extra_field_value(
- $result,
- substr($key, 6),
- $value
- );
- }
- }
- $this->logger->addInfo("Students - User created: ".$row['username']);
- } else {
- $this->logger->addError("Students - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
- $this->logger->addError(strip_tags(Display::getFlashToString()));
- Display::cleanFlashMessages();
- }
- } else {
- if (empty($userInfo)) {
- $this->logger->addError("Students - Can't update user :".$row['username']);
- continue;
- }
- if (isset($row['action']) && $row['action'] === 'delete') {
- // Inactive one year later
- $userInfo['expiration_date'] = api_get_utc_datetime(api_strtotime(time() + $secondsInYear));
- }
- $password = $row['password']; // change password
- $email = $row['email']; // change email
- $resetPassword = 2; // allow password change
- // Conditions that disables the update of password and email:
- if (isset($this->conditions['importStudents'])) {
- if (isset($this->conditions['importStudents']['update']) &&
- isset($this->conditions['importStudents']['update']['avoid'])
- ) {
- // Blocking email update -
- // 1. Condition
- $avoidUsersWithEmail = $this->conditions['importStudents']['update']['avoid']['email'];
- if ($userInfo['email'] != $row['email'] && in_array($row['email'], $avoidUsersWithEmail)) {
- $this->logger->addInfo("Students - User email is not updated : ".$row['username']." because the avoid conditions (email).");
- // Do not change email keep the old email.
- $email = $userInfo['email'];
- }
- // 2. Condition
- if (!in_array($userInfo['email'], $avoidUsersWithEmail) && !in_array($row['email'], $avoidUsersWithEmail)) {
- $email = $userInfo['email'];
- }
- // 3. Condition
- if (in_array($userInfo['email'], $avoidUsersWithEmail) && !in_array($row['email'], $avoidUsersWithEmail)) {
- $email = $row['email'];
- }
- // Blocking password update
- //$avoidUsersWithPassword = $this->conditions['importStudents']['update']['avoid']['password'];
- /*if (isset($row['password'])) {
- $user = api_get_user_entity($userInfo['id']);
- $encoded = UserManager::encryptPassword(
- $row['password'],
- $user
- );
- if ($userInfo['password'] != $encoded &&
- in_array($row['password'], $avoidUsersWithPassword)
- ) {
- $this->logger->addInfo(
- "Students - User password is not updated: ".$row['username']." because the avoid conditions (password)."
- );
- $password = null;
- $resetPassword = 0; // disallow password change
- }
- }*/
- }
- }
- // Always disallow password change during update
- $password = null;
- $resetPassword = 0; // disallow password change
- // Update user
- $result = UserManager::update_user(
- $userInfo['user_id'],
- $row['firstname'], // <<-- changed
- $row['lastname'], // <<-- changed
- $row['username'], // <<-- changed
- $password, //$password = null,
- $row['auth_source'],
- $email,
- STUDENT,
- $userInfo['official_code'],
- $userInfo['phone'],
- $userInfo['picture_uri'],
- $expirationDateOnUpdate,
- $userInfo['active'],
- null, //$creator_id = null,
- 0, //$hr_dept_id = 0,
- null, // $extra = null,
- null, //$language = 'english',
- null, //$encrypt_method = '',
- false, //$send_email = false,
- $resetPassword //$reset_password = 0
- );
- if ($result) {
- if ($row['username'] != $userInfo['username']) {
- $this->logger->addInfo("Students - Username was changes from '".$userInfo['username']."' to '".$row['username']."' ");
- }
- foreach ($row as $key => $value) {
- if (substr($key, 0, 6) === 'extra_') {
- //an extra field
- UserManager::update_extra_field_value(
- $userInfo['user_id'],
- substr($key, 6),
- $value
- );
- }
- }
- $this->logger->addInfo("Students - User updated: ".$row['username']);
- } else {
- $this->logger->addError("Students - User NOT updated: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
- }
- }
- if (($counter % $batchSize) === 0) {
- $em->flush();
- $em->clear(); // Detaches all objects from Doctrine!
- $this->logger->addInfo("Detaches all objects");
- }
- $counter++;
- }
- $em->clear(); // Detaches all objects from Doctrine!
- }
- $timeEnd = microtime(true);
- $executionTime = round(($timeEnd - $timeStart) / 60, 2);
- $this->logger->addInfo("Execution Time for process students: $executionTime Min");
- if ($moveFile) {
- $this->moveFile($file);
- }
- $this->updateUsersEmails();
- }
- /**
- * @param string $file
- */
- private function importCoursesStatic($file, $moveFile, &$teacherBackup = [], &$groupBackup = [])
- {
- $this->importCourses($file, true, $teacherBackup, $groupBackup);
- }
- /**
- * @param string $file
- * @param bool $moveFile
- *
- * @return int
- */
- private function importCalendarStatic($file, $moveFile = true)
- {
- $this->fixCSVFile($file);
- $this->updateUsersEmails();
- $data = Import::csvToArray($file);
- if (!empty($data)) {
- $this->logger->addInfo(count($data).' records found.');
- $eventsToCreate = [];
- $errorFound = false;
- $courseExtraFieldValue = new ExtraFieldValue('course');
- foreach ($data as $row) {
- $sessionId = null;
- $externalSessionId = null;
- if (isset($row['external_sessionID'])) {
- $externalSessionId = $row['external_sessionID'];
- $sessionId = SessionManager::getSessionIdFromOriginalId(
- $externalSessionId,
- $this->extraFieldIdNameList['session']
- );
- }
- $courseCode = null;
- if (isset($row['coursecode'])) {
- $courseCode = $row['coursecode'];
- }
- $courseInfo = api_get_course_info($courseCode);
- $item = $courseExtraFieldValue->get_values_by_handler_and_field_variable(
- $courseInfo['real_id'],
- 'disable_import_calendar'
- );
- if (!empty($item) && isset($item['value']) && $item['value'] == 1) {
- $this->logger->addInfo(
- "Course '".$courseInfo['code']."' has 'disable_import_calendar' turn on. Skip"
- );
- $errorFound = true;
- }
- if (empty($courseInfo)) {
- $this->logger->addInfo("Course '$courseCode' does not exists");
- } else {
- if ($courseInfo['visibility'] == COURSE_VISIBILITY_HIDDEN) {
- $this->logger->addInfo("Course '".$courseInfo['code']."' has hidden visiblity. Skip");
- $errorFound = true;
- }
- }
- if (empty($sessionId)) {
- $this->logger->addInfo("external_sessionID: $externalSessionId does not exists.");
- }
- $teacherId = null;
- $sessionInfo = [];
- if (!empty($sessionId) && !empty($courseInfo)) {
- $sessionInfo = api_get_session_info($sessionId);
- $courseIncluded = SessionManager::relation_session_course_exist(
- $sessionId,
- $courseInfo['real_id']
- );
- if ($courseIncluded == false) {
- $this->logger->addInfo(
- "Course '$courseCode' is not included in session: $sessionId"
- );
- $errorFound = true;
- } else {
- $teachers = CourseManager::get_coach_list_from_course_code(
- $courseInfo['code'],
- $sessionId
- );
- // Getting first teacher.
- if (!empty($teachers)) {
- $teacher = current($teachers);
- $teacherId = $teacher['user_id'];
- } else {
- $teacherId = $sessionInfo['id_coach'];
- }
- }
- } else {
- $errorFound = true;
- }
- if (empty($teacherId)) {
- $errorFound = true;
- $this->logger->addInfo(
- "No teacher found in course code : '$courseCode' and session: '$sessionId'"
- );
- }
- $date = $row['date'];
- $startTime = $row['time_start'];
- $endTime = $row['time_end'];
- $title = $row['title'];
- $comment = $row['comment'];
- $color = isset($row['color']) ? $row['color'] : '';
- $startDateYear = substr($date, 0, 4);
- $startDateMonth = substr($date, 4, 2);
- $startDateDay = substr($date, 6, 8);
- $startDate = $startDateYear.'-'.$startDateMonth.'-'.$startDateDay.' '.$startTime.':00';
- $endDate = $startDateYear.'-'.$startDateMonth.'-'.$startDateDay.' '.$endTime.':00';
- if (!api_is_valid_date($startDate) || !api_is_valid_date($endDate)) {
- $this->logger->addInfo("Verify your dates: '$startDate' : '$endDate' ");
- $errorFound = true;
- }
- // Check session dates
- if ($sessionInfo && !empty($sessionInfo['access_start_date'])) {
- $date = new \DateTime($sessionInfo['access_start_date']);
- $interval = new \DateInterval('P7D');
- $date->sub($interval);
- if ($date->getTimestamp() > time()) {
- $this->logger->addInfo(
- "Calendar event # ".$row['external_calendar_itemID']."
- in session [$externalSessionId] was not added
- because the startdate is more than 7 days in the future: ".$sessionInfo['access_start_date']
- );
- $errorFound = true;
- }
- }
- if ($errorFound == false) {
- $eventsToCreate[] = [
- 'start' => $startDate,
- 'end' => $endDate,
- 'title' => $title,
- 'sender_id' => $teacherId,
- 'course_id' => $courseInfo['real_id'],
- 'session_id' => $sessionId,
- 'comment' => $comment,
- 'color' => $color,
- $this->extraFieldIdNameList['calendar_event'] => $row['external_calendar_itemID'],
- ];
- }
- $errorFound = false;
- }
- if (empty($eventsToCreate)) {
- $this->logger->addInfo('No events to add');
- return 0;
- }
- $extraFieldValue = new ExtraFieldValue('calendar_event');
- $extraFieldName = $this->extraFieldIdNameList['calendar_event'];
- $externalEventId = null;
- $extraField = new ExtraField('calendar_event');
- $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable($extraFieldName);
- if (empty($extraFieldInfo)) {
- $this->logger->addInfo(
- "No calendar event extra field created: $extraFieldName"
- );
- return 0;
- }
- $this->logger->addInfo('Ready to insert # '.count($eventsToCreate).' events');
- $batchSize = $this->batchSize;
- $counter = 1;
- $em = Database::getManager();
- $eventStartDateList = [];
- $eventEndDateList = [];
- $report = [
- 'mail_sent' => 0,
- 'mail_not_sent_announcement_exists' => 0,
- 'mail_not_sent_because_date' => 0,
- ];
- $eventsToCreateFinal = [];
- foreach ($eventsToCreate as $event) {
- $update = false;
- $item = null;
- if (!isset($event[$extraFieldName])) {
- $this->logger->addInfo('No external_calendar_itemID found. Skipping ...');
- continue;
- } else {
- $externalEventId = $event[$extraFieldName];
- if (empty($externalEventId)) {
- $this->logger->addInfo('external_calendar_itemID was set but empty. Skipping ...');
- continue;
- }
- $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
- $extraFieldName,
- $externalEventId,
- false,
- false,
- false
- );
- if (!empty($item)) {
- $update = true;
- }
- }
- $courseInfo = api_get_course_info_by_id($event['course_id']);
- $event['course_info'] = $courseInfo;
- $event['update'] = $update;
- $event['item'] = $item;
- $calendarEvent = null;
- /* Check if event changed of course code */
- if (!empty($item) && isset($item['item_id']) && !empty($item['item_id'])) {
- /** @var CCalendarEvent $calendarEvent */
- $calendarEvent = $em->getRepository('ChamiloCourseBundle:CCalendarEvent')->find($item['item_id']);
- }
- if ($calendarEvent) {
- $this->logger->addInfo('Calendar event found '.$item['item_id']);
- if ($calendarEvent->getCId() != $courseInfo['real_id']) {
- $this->logger->addInfo('Move from course #'.$calendarEvent->getCId().' to #'.$courseInfo['real_id']);
- // Seems that the course id changed in the csv
- $calendarEvent->setCId($courseInfo['real_id']);
- $em->persist($calendarEvent);
- $em->flush();
- $criteria = [
- 'tool' => 'calendar_event',
- 'ref' => $item['item_id'],
- ];
- /** @var CItemProperty $itemProperty */
- $itemProperty = $em->getRepository('ChamiloCourseBundle:CItemProperty')->findOneBy($criteria);
- $courseEntity = $em->getRepository('ChamiloCoreBundle:Course')->find($courseInfo['real_id']);
- if ($itemProperty && $courseEntity) {
- $itemProperty->setCourse($courseEntity);
- $em->persist($itemProperty);
- $em->flush();
- }
- }
- // Checking if session still exists
- $calendarSessionId = (int) $calendarEvent->getSessionId();
- if (!empty($calendarSessionId)) {
- $calendarSessionInfo = api_get_session_info($calendarSessionId);
- if (empty($calendarSessionInfo)) {
- $calendarId = (int) $calendarEvent->getIid();
- // Delete calendar events because the session was deleted!
- $this->logger->addInfo(
- "Delete event # $calendarId because session # $calendarSessionId doesn't exist"
- );
- $sql = "DELETE FROM c_calendar_event
- WHERE iid = $calendarId AND session_id = $calendarSessionId";
- Database::query($sql);
- $this->logger->addInfo($sql);
- $sql = "DELETE FROM c_item_property
- WHERE
- tool = 'calendar_event' AND
- ref = $calendarSessionId AND
- session_id = $calendarSessionId";
- Database::query($sql);
- $this->logger->addInfo($sql);
- }
- }
- } else {
- $this->logger->addInfo('Calendar event not found '.$item['item_id']);
- }
- $event['external_event_id'] = $externalEventId;
- if (isset($eventStartDateList[$courseInfo['real_id']]) &&
- isset($eventStartDateList[$courseInfo['real_id']][$event['session_id']])
- ) {
- $currentItemDate = api_strtotime($event['start']);
- $firstDate = $eventStartDateList[$courseInfo['real_id']][$event['session_id']];
- if ($currentItemDate < api_strtotime($firstDate)) {
- $eventStartDateList[$courseInfo['real_id']][$event['session_id']] = $event['start'];
- $eventEndDateList[$courseInfo['real_id']][$event['session_id']] = $event['end'];
- }
- } else {
- // First time
- $eventStartDateList[$courseInfo['real_id']][$event['session_id']] = $event['start'];
- $eventEndDateList[$courseInfo['real_id']][$event['session_id']] = $event['end'];
- }
- $eventsToCreateFinal[] = $event;
- }
- $eventAlreadySent = [];
- $tpl = new Template(null, false, false, false, false, false, false);
- foreach ($eventsToCreateFinal as $event) {
- $courseInfo = $event['course_info'];
- $item = $event['item'];
- $update = $event['update'];
- $externalEventId = $event['external_event_id'];
- $info = 'Course: '.$courseInfo['real_id'].' ('.$courseInfo['code'].') - Session: '.$event['session_id'].' external event id: '.$externalEventId;
- $agenda = new Agenda(
- 'course',
- $event['sender_id'],
- $courseInfo['real_id'],
- $event['session_id']
- );
- $agenda->set_course($courseInfo);
- $agenda->setSessionId($event['session_id']);
- $agenda->setSenderId($event['sender_id']);
- $agenda->setIsAllowedToEdit(true);
- $eventComment = $event['comment'];
- $color = $event['color'];
- // To use the event comment you need
- // ALTER TABLE c_calendar_event ADD COLUMN comment TEXT;
- // add in configuration.php allow_agenda_event_comment = true
- if (empty($courseInfo)) {
- $this->logger->addInfo(
- "No course found for event #$externalEventId Course #".$event['course_id']." Skipping ..."
- );
- continue;
- }
- if (empty($event['sender_id'])) {
- $this->logger->addInfo(
- "No sender found for event #$externalEventId Send #".$event['sender_id']." Skipping ..."
- );
- continue;
- }
- // Taking first element of course-session event
- $alreadyAdded = false;
- $firstDate = $eventStartDateList[$courseInfo['real_id']][$event['session_id']];
- $firstEndDate = $eventEndDateList[$courseInfo['real_id']][$event['session_id']];
- if (isset($eventAlreadySent[$courseInfo['real_id']]) &&
- isset($eventAlreadySent[$courseInfo['real_id']][$event['session_id']])
- ) {
- $alreadyAdded = true;
- } else {
- $eventAlreadySent[$courseInfo['real_id']][$event['session_id']] = true;
- }
- // Working days (Mon-Fri)see BT#12156#note-16
- $days = 3;
- $startDatePlusDays = api_strtotime("$days weekdays");
- /*
- $timePart = date('H:i:s', api_strtotime('now'));
- $datePart = date('Y-m-d', api_strtotime("$days weekdays"));
- $startDatePlusDays = "$timePart $datePart";
- */
- $this->logger->addInfo(
- 'startDatePlusDays: '.api_get_utc_datetime($startDatePlusDays).' - First date: '.$firstDate
- );
- // Send
- $sendMail = false;
- if ($startDatePlusDays > api_strtotime($firstDate)) {
- $sendMail = true;
- }
- // Send announcement to users
- if ($sendMail && $alreadyAdded == false) {
- $start = $firstDate;
- $end = $firstEndDate;
- if (!empty($end) &&
- api_format_date($start, DATE_FORMAT_LONG) ==
- api_format_date($end, DATE_FORMAT_LONG)
- ) {
- $date = api_format_date($start, DATE_FORMAT_LONG).' ('.
- api_format_date($start, TIME_NO_SEC_FORMAT).' '.
- api_format_date($end, TIME_NO_SEC_FORMAT).')';
- } else {
- $date = api_format_date($start, DATE_TIME_FORMAT_LONG_24H).' - '.
- api_format_date($end, DATE_TIME_FORMAT_LONG_24H);
- }
- $sessionName = '';
- $sessionId = isset($event['session_id']) && !empty($event['session_id']) ? $event['session_id'] : 0;
- if (!empty($sessionId)) {
- $sessionName = api_get_session_name($sessionId);
- }
- $courseTitle = $courseInfo['title'];
- $sessionExtraFieldValue = new ExtraFieldValue('session');
- $values = $sessionExtraFieldValue->get_values_by_handler_and_field_variable(
- $event['session_id'],
- $this->extraFieldIdNameList['session_career']
- );
- $careerName = '';
- if (!empty($values)) {
- foreach ($values as $value) {
- if (isset($value['value'])) {
- $careerName = $value['value'];
- }
- }
- }
- $subject = sprintf(
- get_lang('WelcomeToPortalXInCourseSessionX'),
- api_get_setting('siteName'),
- $courseInfo['title']
- );
- $tpl->assign('site_name', api_get_setting('siteName'));
- $tpl->assign('course_title', $courseTitle);
- $tpl->assign('career_name', $careerName);
- $tpl->assign('first_lesson', $date);
- $tpl->assign('location', $eventComment);
- $tpl->assign('session_name', $sessionName);
- if (empty($sessionId)) {
- $teachersToString = CourseManager::getTeacherListFromCourseCodeToString($courseInfo['code'], ',');
- } else {
- $teachersToString = SessionManager::getCoachesByCourseSessionToString(
- $sessionId,
- $courseInfo['real_id'],
- ','
- );
- }
- $tpl->assign('teachers', $teachersToString);
- $templateName = $tpl->get_template('mail/custom_calendar_welcome.tpl');
- $emailBody = $tpl->fetch($templateName);
- $coaches = SessionManager::getCoachesByCourseSession(
- $event['session_id'],
- $courseInfo['real_id']
- );
- // Search if an announcement exists:
- $announcementsWithTitleList = AnnouncementManager::getAnnouncementsByTitle(
- $subject,
- $courseInfo['real_id'],
- $event['session_id'],
- 1
- );
- if (count($announcementsWithTitleList) === 0) {
- $this->logger->addInfo(
- 'Mail to be sent because start date: '.$event['start'].' and no announcement found.'
- );
- $senderId = $this->defaultAdminId;
- if (!empty($coaches) && isset($coaches[0]) && !empty($coaches[0])) {
- $senderId = $coaches[0];
- }
- $announcementId = AnnouncementManager::add_announcement(
- $courseInfo,
- $event['session_id'],
- $subject,
- $emailBody,
- [
- 'everyone',
- 'users' => $coaches,
- ],
- [],
- null,
- null,
- false,
- $senderId
- );
- if ($announcementId) {
- $this->logger->addInfo("Announcement added: $announcementId in $info");
- $this->logger->addInfo("<<--SENDING MAIL Sender id: $senderId-->>");
- $report['mail_sent']++;
- AnnouncementManager::sendEmail(
- $courseInfo,
- $event['session_id'],
- $announcementId,
- false,
- false,
- $this->logger,
- $senderId,
- true
- );
- } else {
- $this->logger->addError(
- "Error when trying to add announcement with title $subject here: $info and SenderId = $senderId"
- );
- }
- } else {
- $report['mail_not_sent_announcement_exists']++;
- $this->logger->addInfo(
- "Mail NOT sent. An announcement seems to be already saved in '$info'"
- );
- }
- } else {
- $this->logger->addInfo(
- "Send Mail: ".intval($sendMail).' - Already added: '.intval($alreadyAdded)
- );
- if ($sendMail == false) {
- $report['mail_not_sent_because_date']++;
- }
- }
- $content = '';
- if ($update && isset($item['item_id'])) {
- $eventInfo = $agenda->get_event($item['item_id']);
- if (empty($eventInfo)) {
- // Means that agenda external id exists but the event doesn't exist
- $this->logger->addInfo("external event id exists: $externalEventId");
- $this->logger->addInfo("but Chamilo event doesn't exists: ".$item['item_id']);
- $eventId = $agenda->addEvent(
- $event['start'],
- $event['end'],
- false,
- $event['title'],
- $content,
- ['everyone'], // $usersToSend
- false, //$addAsAnnouncement = false
- null, // $parentEventId
- [], //$attachmentArray = array(),
- [], //$attachmentCommentList
- $eventComment,
- $color
- );
- if (!empty($eventId)) {
- $this->logger->addInfo("Chamilo event created: ".$eventId);
- $extraFieldValueItem = $extraFieldValue->get_values_by_handler_and_field_id(
- $item['item_id'],
- $extraFieldInfo['id']
- );
- if (!empty($extraFieldValueItem) && isset($extraFieldValueItem['id'])) {
- $params = [
- 'id' => $extraFieldValueItem['id'],
- 'item_id' => $eventId,
- ];
- $extraFieldValue->update($params);
- $this->logger->addInfo(
- 'Updating calendar extra field #'.$extraFieldValueItem['id'].'
- new item_id: '.$eventId.' old item_id: '.$item['item_id']
- );
- }
- } else {
- $this->logger->addInfo("Error while creating event external id: $externalEventId");
- }
- } else {
- // The event already exists, just update
- $eventResult = $agenda->editEvent(
- $item['item_id'],
- $event['start'],
- $event['end'],
- false,
- $event['title'],
- $content,
- ['everyone'], // $usersToSend
- [], //$attachmentArray = array(),
- [], //$attachmentCommentList
- $eventComment,
- $color,
- false,
- false,
- $this->defaultAdminId
- );
- if ($eventResult !== false) {
- $this->logger->addInfo(
- "Event updated #".$item['item_id']." External cal Id: (".$externalEventId.") $info"
- );
- } else {
- $this->logger->addInfo(
- "Error while updating event with external id: $externalEventId"
- );
- }
- }
- } else {
- // New event. Create it.
- $eventId = $agenda->addEvent(
- $event['start'],
- $event['end'],
- false,
- $event['title'],
- $content,
- ['everyone'], // $usersToSend
- false, //$addAsAnnouncement = false
- null, // $parentEventId
- [], //$attachmentArray = array(),
- [], //$attachmentCommentList
- $eventComment,
- $color
- );
- if (!empty($eventId)) {
- $extraFieldValue->save(
- [
- 'value' => $externalEventId,
- 'field_id' => $extraFieldInfo['id'],
- 'item_id' => $eventId,
- ]
- );
- $this->logger->addInfo(
- "Event added: #$eventId External cal id: (".$externalEventId.") $info"
- );
- } else {
- $this->logger->addInfo(
- "Error while creating event external id: $externalEventId"
- );
- }
- }
- if (($counter % $batchSize) === 0) {
- $em->flush();
- $em->clear(); // Detaches all objects from Doctrine!
- }
- $counter++;
- }
- $em->clear(); // Detaches all objects from Doctrine!
- $this->logger->addInfo('------Summary------');
- foreach ($report as $title => $count) {
- $this->logger->addInfo("$title: $count");
- }
- $this->logger->addInfo('------End Summary------');
- }
- if ($moveFile) {
- $this->moveFile($file);
- }
- }
- /**
- * @param string $file
- * @param bool $moveFile
- * @param array $teacherBackup
- * @param array $groupBackup
- */
- private function importCourses(
- $file,
- $moveFile = true,
- &$teacherBackup = [],
- &$groupBackup = []
- ) {
- $this->fixCSVFile($file);
- $data = Import::csvToArray($file);
- if (!empty($data)) {
- $this->logger->addInfo(count($data)." records found.");
- foreach ($data as $row) {
- $row = $this->cleanCourseRow($row);
- $courseId = CourseManager::get_course_id_from_original_id(
- $row['extra_'.$this->extraFieldIdNameList['course']],
- $this->extraFieldIdNameList['course']
- );
- $courseInfo = api_get_course_info_by_id($courseId);
- if (empty($courseInfo)) {
- // Create
- $params = [];
- $params['title'] = $row['title'];
- $params['exemplary_content'] = false;
- $params['wanted_code'] = $row['course_code'];
- $params['course_category'] = $row['course_category'];
- $params['course_language'] = $row['language'];
- $params['teachers'] = $row['teachers'];
- $params['visibility'] = $row['visibility'];
- $courseInfo = CourseManager::create_course(
- $params,
- $this->defaultAdminId
- );
- if (!empty($courseInfo)) {
- CourseManager::update_course_extra_field_value(
- $courseInfo['code'],
- 'external_course_id',
- $row['extra_'.$this->extraFieldIdNameList['course']]
- );
- $this->logger->addInfo("Courses - Course created ".$courseInfo['code']);
- } else {
- $this->logger->addError("Courses - Can't create course:".$row['title']);
- }
- } else {
- // Update
- $params = [
- 'title' => $row['title'],
- 'category_code' => $row['course_category'],
- 'visibility' => $row['visibility'],
- ];
- $result = CourseManager::update_attributes(
- $courseInfo['real_id'],
- $params
- );
- $addTeacherToSession = isset($courseInfo['add_teachers_to_sessions_courses']) && !empty($courseInfo['add_teachers_to_sessions_courses']) ? true : false;
- $teachers = $row['teachers'];
- if (!is_array($teachers)) {
- $teachers = [$teachers];
- }
- if ($addTeacherToSession) {
- $this->logger->addInfo("Add teacher to all course sessions");
- CourseManager::updateTeachers(
- $courseInfo,
- $row['teachers'],
- false,
- true,
- false,
- $teacherBackup,
- $this->logger
- );
- } else {
- CourseManager::updateTeachers(
- $courseInfo,
- $row['teachers'],
- true,
- false,
- false,
- $teacherBackup,
- $this->logger
- );
- }
- foreach ($teachers as $teacherId) {
- if (isset($groupBackup['tutor'][$teacherId]) &&
- isset($groupBackup['tutor'][$teacherId][$courseInfo['code']])
- ) {
- foreach ($groupBackup['tutor'][$teacherId][$courseInfo['code']] as $data) {
- $groupInfo = GroupManager::get_group_properties($data['group_id']);
- GroupManager::subscribe_tutors(
- [$teacherId],
- $groupInfo,
- $data['c_id']
- );
- }
- }
- if (isset($groupBackup['user'][$teacherId]) &&
- isset($groupBackup['user'][$teacherId][$courseInfo['code']]) &&
- !empty($groupBackup['user'][$teacherId][$courseInfo['code']])
- ) {
- foreach ($groupBackup['user'][$teacherId][$courseInfo['code']] as $data) {
- $groupInfo = GroupManager::get_group_properties($data['group_id']);
- GroupManager::subscribe_users(
- [$teacherId],
- $groupInfo,
- $data['c_id']
- );
- }
- }
- }
- if ($result) {
- $this->logger->addInfo("Courses - Course updated ".$courseInfo['code']);
- } else {
- $this->logger->addError("Courses - Course NOT updated ".$courseInfo['code']);
- }
- }
- }
- }
- if ($moveFile) {
- $this->moveFile($file);
- }
- }
- /**
- * Parse filename: encora_subsessionsextid-static_31082016.csv.
- *
- * @param string $file
- */
- private function importSubscribeUserToCourseSessionExtStatic($file, $moveFile = true)
- {
- $data = Import::csv_reader($file);
- if (!empty($data)) {
- $this->logger->addInfo(count($data)." records found.");
- $userIdList = [];
- foreach ($data as $row) {
- $chamiloUserName = $row['UserName'];
- $chamiloCourseCode = $row['CourseCode'];
- $externalSessionId = $row['ExtSessionID'];
- $status = $row['Status'];
- $chamiloSessionId = null;
- if (!empty($externalSessionId)) {
- $chamiloSessionId = SessionManager::getSessionIdFromOriginalId(
- $externalSessionId,
- $this->extraFieldIdNameList['session']
- );
- }
- $sessionInfo = api_get_session_info($chamiloSessionId);
- if (empty($sessionInfo)) {
- $this->logger->addError('Session does not exists: '.$chamiloSessionId);
- continue;
- }
- $courseInfo = api_get_course_info($chamiloCourseCode);
- if (empty($courseInfo)) {
- $this->logger->addError('Course does not exists: '.$courseInfo);
- continue;
- }
- $userId = UserManager::get_user_id_from_username($chamiloUserName);
- if (empty($userId)) {
- $this->logger->addError('User does not exists: '.$chamiloUserName);
- continue;
- }
- switch ($status) {
- case 'student':
- SessionManager::subscribe_users_to_session_course(
- [$userId],
- $chamiloSessionId,
- $courseInfo['code']
- );
- break;
- case 'teacher':
- SessionManager::set_coach_to_course_session(
- $userId,
- $chamiloSessionId,
- $courseInfo['code']
- );
- break;
- case 'drh':
- $removeAllSessionsFromUser = true;
- if (in_array($userId, $userIdList)) {
- $removeAllSessionsFromUser = false;
- } else {
- $userIdList[] = $userId;
- }
- $userInfo = api_get_user_info($userId);
- SessionManager::subscribeSessionsToDrh(
- $userInfo,
- [$chamiloSessionId],
- false,
- $removeAllSessionsFromUser
- );
- break;
- }
- $this->logger->addError(
- "User '$chamiloUserName' was added as '$status' to Session: #$chamiloSessionId - Course: ".$courseInfo['code']
- );
- }
- }
- if ($moveFile) {
- $this->moveFile($file);
- }
- }
- /**
- * @param $file
- * @param bool $moveFile
- */
- private function importUnsubSessionsExtIdStatic($file, $moveFile = true)
- {
- $data = Import::csv_reader($file);
- if (!empty($data)) {
- $this->logger->addInfo(count($data)." records found.");
- foreach ($data as $row) {
- $chamiloUserName = $row['UserName'];
- $chamiloCourseCode = $row['CourseCode'];
- $externalSessionId = $row['ExtSessionID'];
- $dateStop = $row['DateStop'];
- $chamiloSessionId = null;
- if (!empty($externalSessionId)) {
- $chamiloSessionId = SessionManager::getSessionIdFromOriginalId(
- $externalSessionId,
- $this->extraFieldIdNameList['session']
- );
- }
- $sessionInfo = api_get_session_info($chamiloSessionId);
- if (empty($sessionInfo)) {
- $this->logger->addError('Session does not exists: '.$chamiloSessionId);
- continue;
- }
- $courseInfo = api_get_course_info($chamiloCourseCode);
- if (empty($courseInfo)) {
- $this->logger->addError('Course does not exists: '.$courseInfo);
- continue;
- }
- $userId = UserManager::get_user_id_from_username($chamiloUserName);
- if (empty($userId)) {
- $this->logger->addError('User does not exists: '.$chamiloUserName);
- continue;
- }
- SessionManager::removeUsersFromCourseSession(
- [$userId],
- $chamiloSessionId,
- $courseInfo
- );
- $this->logger->addError(
- "User '$chamiloUserName' was remove from Session: #$chamiloSessionId - Course: ".$courseInfo['code']
- );
- }
- }
- if ($moveFile) {
- $this->moveFile($file);
- }
- }
- /**
- * @param string $file
- */
- private function importSessionsExtIdStatic($file, $moveFile = true)
- {
- $data = Import::csv_reader($file);
- if (!empty($data)) {
- $this->logger->addInfo(count($data)." records found.");
- foreach ($data as $row) {
- $chamiloUserName = $row['UserName'];
- $chamiloCourseCode = $row['CourseCode'];
- $externalSessionId = $row['ExtSessionID'];
- $type = $row['Type'];
- $chamiloSessionId = null;
- if (!empty($externalSessionId)) {
- $chamiloSessionId = SessionManager::getSessionIdFromOriginalId(
- $externalSessionId,
- $this->extraFieldIdNameList['session']
- );
- }
- $sessionInfo = api_get_session_info($chamiloSessionId);
- if (empty($sessionInfo)) {
- $this->logger->addError('Session does not exists: '.$chamiloSessionId);
- continue;
- }
- $courseInfo = api_get_course_info($chamiloCourseCode);
- if (empty($courseInfo)) {
- $this->logger->addError('Course does not exists: '.$courseInfo);
- continue;
- }
- $userId = UserManager::get_user_id_from_username($chamiloUserName);
- if (empty($userId)) {
- $this->logger->addError('User does not exists: '.$chamiloUserName);
- continue;
- }
- switch ($type) {
- case 'student':
- SessionManager::subscribe_users_to_session_course(
- [$userId],
- $chamiloSessionId,
- $courseInfo['code'],
- null,
- false
- );
- break;
- case 'teacher':
- SessionManager::set_coach_to_course_session(
- $userId,
- $chamiloSessionId,
- $courseInfo['code']
- );
- break;
- }
- $this->logger->addError(
- "User '$chamiloUserName' with status $type was added to session: #$chamiloSessionId - Course: ".$courseInfo['code']
- );
- }
- }
- if ($moveFile) {
- $this->moveFile($file);
- }
- }
- /**
- * Updates the session synchronize with the csv file.
- *
- * @param bool $moveFile
- * @param string $file
- */
- private function importSessionsStatic($file, $moveFile = true)
- {
- $content = file($file);
- $sessions = [];
- $tag_names = [];
- foreach ($content as $key => $enreg) {
- $enreg = explode(';', trim($enreg));
- if ($key) {
- foreach ($tag_names as $tag_key => $tag_name) {
- if (isset($enreg[$tag_key])) {
- $sessions[$key - 1][$tag_name] = $enreg[$tag_key];
- }
- }
- } else {
- foreach ($enreg as $tag_name) {
- $tag_names[] = api_preg_replace(
- '/[^a-zA-Z0-9_\-]/',
- '',
- $tag_name
- );
- }
- if (!in_array('SessionName', $tag_names) ||
- !in_array('DateStart', $tag_names) || !in_array('DateEnd', $tag_names)
- ) {
- $error_message = get_lang('NoNeededData');
- break;
- }
- }
- }
- if (!empty($sessions)) {
- // Looping the sessions.
- foreach ($sessions as $session) {
- if (!empty($session['SessionID'])) {
- $sessionId = SessionManager::getSessionIdFromOriginalId(
- $session['SessionID'],
- $this->extraFieldIdNameList['session']
- );
- $coachUserName = isset($session['Coach']) ? $session['Coach'] : null;
- $categoryId = isset($session['category_id']) ? $session['category_id'] : null;
- // 2014-06-30
- $dateStart = explode('/', $session['DateStart']);
- $dateEnd = explode('/', $session['DateEnd']);
- $visibility = $this->defaultSessionVisibility;
- $coachId = null;
- if (!empty($coachUserName)) {
- $coachInfo = api_get_user_info_from_username($coachUserName);
- $coachId = $coachInfo['user_id'];
- }
- $dateStart = $dateStart[0].'-'.$dateStart[1].'-'.$dateStart[2].' 00:00:00';
- $dateEnd = $dateEnd[0].'-'.$dateEnd[1].'-'.$dateEnd[2].' 23:59:59';
- $date = new \DateTime($dateStart);
- $interval = new DateInterval('P'.$this->daysCoachAccessBeforeBeginning.'D');
- $date->sub($interval);
- $coachBefore = $date->format('Y-m-d h:i');
- $date = new \DateTime($dateEnd);
- $interval = new DateInterval('P'.$this->daysCoachAccessAfterBeginning.'D');
- $date->add($interval);
- $coachAfter = $date->format('Y-m-d h:i');
- /*$dateStart = api_get_utc_datetime($dateStart);
- $dateEnd = api_get_utc_datetime($dateEnd);
- $coachBefore = api_get_utc_datetime($coachBefore);
- $coachAfter = api_get_utc_datetime($coachAfter);*/
- if (empty($sessionId)) {
- $result = SessionManager::create_session(
- $session['SessionName'],
- $dateStart,
- $dateEnd,
- $dateStart,
- $dateEnd,
- $coachBefore,
- $coachAfter,
- $coachId,
- $categoryId,
- $visibility
- );
- if (is_numeric($result)) {
- $sessionId = $result;
- $this->logger->addInfo("Session #$sessionId created: ".$session['SessionName']);
- SessionManager::update_session_extra_field_value(
- $sessionId,
- $this->extraFieldIdNameList['session'],
- $session['SessionID']
- );
- } else {
- $this->logger->addInfo("Failed creating session: ".$session['SessionName']);
- }
- } else {
- $sessionInfo = api_get_session_info($sessionId);
- $accessBefore = null;
- $accessAfter = null;
- if (empty($sessionInfo['nb_days_access_before_beginning']) ||
- (!empty($sessionInfo['nb_days_access_before_beginning']) &&
- $sessionInfo['nb_days_access_before_beginning'] < $this->daysCoachAccessBeforeBeginning)
- ) {
- $accessBefore = $coachBefore;
- }
- $accessAfter = null;
- if (empty($sessionInfo['nb_days_access_after_end']) ||
- (!empty($sessionInfo['nb_days_access_after_end']) &&
- $sessionInfo['nb_days_access_after_end'] < $this->daysCoachAccessAfterBeginning)
- ) {
- $accessAfter = $coachAfter;
- }
- $showDescription = isset($sessionInfo['show_description']) ? $sessionInfo['show_description'] : 1;
- $result = SessionManager::edit_session(
- $sessionId,
- $session['SessionName'],
- $dateStart,
- $dateEnd,
- $dateStart,
- $dateEnd,
- $accessBefore,
- $accessAfter,
- $coachId,
- $categoryId,
- $visibility,
- null, //$description = null,
- $showDescription
- );
- if (is_numeric($result)) {
- $this->logger->addInfo("Session #$sessionId updated: ".$session['SessionName']);
- $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
- $params = [
- 'description' => $session['SessionDescription'],
- ];
- Database::update(
- $tbl_session,
- $params,
- ['id = ?' => $sessionId]
- );
- }
- }
- if (!empty($sessionId)) {
- // Courses
- $courses = explode('|', $session['Courses']);
- $courseList = [];
- $courseListWithCoach = [];
- foreach ($courses as $course) {
- $courseArray = bracketsToArray($course);
- $courseCode = $courseArray[0];
- if (CourseManager::course_exists($courseCode)) {
- $courseInfo = api_get_course_info($courseCode);
- $courseList[] = $courseInfo['real_id'];
- // Extracting course coaches
- $courseCoaches = isset($courseArray[1]) ? $courseArray[1] : null;
- $courseCoaches = explode(',', $courseCoaches);
- // Extracting students
- $courseUsers = isset($courseArray[2]) ? $courseArray[2] : null;
- $courseUsers = explode(',', $courseUsers);
- $courseListWithCoach[] = [
- 'course_info' => $courseInfo,
- 'coaches' => $courseCoaches,
- 'course_users' => $courseUsers,
- ];
- }
- }
- SessionManager::add_courses_to_session(
- $sessionId,
- $courseList,
- true
- );
- $this->logger->addInfo("Session #$sessionId: Courses added: '".implode(', ', $courseList)."'");
- if (empty($courseListWithCoach)) {
- $this->logger->addInfo("No users/coaches to update");
- continue;
- }
- foreach ($courseListWithCoach as $courseData) {
- $courseInfo = $courseData['course_info'];
- $courseCode = $courseInfo['code'];
- $courseId = $courseInfo['real_id'];
- $courseCoaches = $courseData['coaches'];
- $courseUsers = $courseData['course_users'];
- // Coaches
- if (!empty($courseCoaches)) {
- $coachList = [];
- foreach ($courseCoaches as $courseCoach) {
- $courseCoachId = UserManager::get_user_id_from_username(
- $courseCoach
- );
- if ($courseCoachId !== false) {
- // Just insert new coaches
- $coachList[] = $courseCoachId;
- }
- }
- $this->logger->addInfo("Session #$sessionId: course '$courseCode' coaches added: '".implode(', ', $coachList)."'");
- SessionManager::updateCoaches(
- $sessionId,
- $courseId,
- $coachList,
- true
- );
- } else {
- $this->logger->addInfo("No coaches added");
- }
- // Students
- if (!empty($courseUsers)) {
- $userList = [];
- foreach ($courseUsers as $username) {
- $userInfo = api_get_user_info_from_username(trim($username));
- if (!empty($userInfo)) {
- $userList[] = $userInfo['user_id'];
- }
- }
- $this->logger->addInfo("Session #$sessionId: course '$courseCode': Students added '".implode(', ', $userList)."'");
- SessionManager::subscribe_users_to_session_course(
- $userList,
- $sessionId,
- $courseCode,
- SESSION_VISIBLE_READ_ONLY,
- true
- );
- } else {
- $this->logger->addInfo("No users to register.");
- }
- }
- } else {
- $this->logger->addInfo(
- 'SessionID not found in system.'
- );
- }
- } else {
- $this->logger->addInfo('SessionID does not exists');
- }
- }
- } else {
- $this->logger->addInfo($error_message);
- }
- if ($moveFile) {
- $this->moveFile($file);
- }
- }
- /**
- * @param $file
- * @param bool $moveFile
- * @param array $teacherBackup
- * @param array $groupBackup
- */
- private function importOpenSessions(
- $file,
- $moveFile = true,
- &$teacherBackup = [],
- &$groupBackup = []
- ) {
- $this->importSessions($file, $moveFile, $teacherBackup, $groupBackup);
- }
- /**
- * @param string $file
- * @param bool $moveFile
- * @param array $teacherBackup
- * @param array $groupBackup
- */
- private function importSessions(
- $file,
- $moveFile = true,
- &$teacherBackup = [],
- &$groupBackup = []
- ) {
- $avoid = null;
- if (isset($this->conditions['importSessions']) &&
- isset($this->conditions['importSessions']['update'])
- ) {
- $avoid = $this->conditions['importSessions']['update'];
- }
- $result = SessionManager::importCSV(
- $file,
- true,
- $this->defaultAdminId,
- $this->logger,
- [
- 'SessionID' => 'extra_'.$this->extraFieldIdNameList['session'],
- 'CareerId' => 'extra_'.$this->extraFieldIdNameList['session_career'],
- ],
- $this->extraFieldIdNameList['session'],
- $this->daysCoachAccessBeforeBeginning,
- $this->daysCoachAccessAfterBeginning,
- $this->defaultSessionVisibility,
- $avoid,
- false, // deleteUsersNotInList
- false, // updateCourseCoaches
- true, // sessionWithCoursesModifier
- true, //$addOriginalCourseTeachersAsCourseSessionCoaches
- false, //$removeAllTeachersFromCourse
- 1, // $showDescription,
- $teacherBackup,
- $groupBackup
- );
- if (!empty($result['error_message'])) {
- $this->logger->addError($result['error_message']);
- }
- $this->logger->addInfo("Sessions - Sessions parsed: ".$result['session_counter']);
- if ($moveFile) {
- $this->moveFile($file);
- }
- }
- /**
- * @param string $file
- * @param bool $moveFile
- */
- private function importSubscribeStatic($file, $moveFile = true)
- {
- $data = Import::csv_reader($file);
- if (!empty($data)) {
- $this->logger->addInfo(count($data)." records found.");
- foreach ($data as $row) {
- $chamiloUserName = $row['UserName'];
- $chamiloCourseCode = $row['CourseCode'];
- $chamiloSessionId = $row['SessionID'];
- $type = $row['Type'];
- $sessionInfo = api_get_session_info($chamiloSessionId);
- if (empty($sessionInfo)) {
- $this->logger->addError('Session does not exists: '.$chamiloSessionId);
- continue;
- }
- $courseInfo = api_get_course_info($chamiloCourseCode);
- if (empty($courseInfo)) {
- $this->logger->addError('Course does not exists: '.$courseInfo);
- continue;
- }
- $userId = UserManager::get_user_id_from_username($chamiloUserName);
- if (empty($userId)) {
- $this->logger->addError('User does not exists: '.$chamiloUserName);
- continue;
- }
- switch ($type) {
- case 'student':
- SessionManager::subscribe_users_to_session_course(
- [$userId],
- $chamiloSessionId,
- $courseInfo['code'],
- null,
- false
- );
- break;
- case 'teacher':
- SessionManager::set_coach_to_course_session(
- $userId,
- $chamiloSessionId,
- $courseInfo['real_id']
- );
- break;
- }
- $this->logger->addError(
- "User '$chamiloUserName' with status $type was added to session: #$chamiloSessionId - Course: ".$courseInfo['code']
- );
- }
- }
- if ($moveFile) {
- $this->moveFile($file);
- }
- }
- /**
- * @param $file
- * @param bool $moveFile
- */
- private function importSubscribeUserToCourse($file, $moveFile = false, &$teacherBackup = [])
- {
- $data = Import::csv_reader($file);
- if (!empty($data)) {
- $this->logger->addInfo(count($data)." records found.");
- foreach ($data as $row) {
- $chamiloUserName = $row['UserName'];
- $chamiloCourseCode = $row['CourseCode'];
- $status = $row['Status'];
- $courseInfo = api_get_course_info($chamiloCourseCode);
- if (empty($courseInfo)) {
- $this->logger->addError(
- 'Course does not exists: '.$chamiloCourseCode
- );
- continue;
- }
- $userId = UserManager::get_user_id_from_username(
- $chamiloUserName
- );
- if (empty($userId)) {
- $this->logger->addError(
- 'User does not exists: '.$chamiloUserName
- );
- continue;
- }
- $userCourseCategory = '';
- if (isset($teacherBackup[$userId]) &&
- isset($teacherBackup[$userId][$courseInfo['code']])
- ) {
- $courseUserData = $teacherBackup[$userId][$courseInfo['code']];
- $userCourseCategory = $courseUserData['user_course_cat'];
- }
- $result = CourseManager::subscribeUser(
- $userId,
- $courseInfo['code'],
- $status,
- 0,
- $userCourseCategory
- );
- if ($result) {
- $this->logger->addInfo(
- "User $userId added to course ".$courseInfo['code']." with status '$status' with course category: '$userCourseCategory'"
- );
- } else {
- $this->logger->addInfo(
- "User $userId was NOT ADDED to course ".$courseInfo['code']." with status '$status' with course category: '$userCourseCategory'"
- );
- }
- }
- }
- if ($moveFile) {
- $this->moveFile($file);
- }
- }
- /**
- * 23/4/2017 to datetime.
- *
- * @param $string
- *
- * @return mixed
- */
- private function createDateTime($string)
- {
- if (empty($string)) {
- return null;
- }
- $date = DateTime::createFromFormat('j/m/Y', $string);
- if ($date) {
- return $date;
- }
- return null;
- }
- /**
- * @param $file
- * @param bool $moveFile
- * @param array $teacherBackup
- * @param array $groupBackup
- *
- * @return bool
- */
- private function importCareers(
- $file,
- $moveFile = false,
- &$teacherBackup = [],
- &$groupBackup = []
- ) {
- $data = Import::csv_reader($file);
- if (!empty($data)) {
- $this->logger->addInfo(count($data)." records found.");
- $extraFieldValue = new ExtraFieldValue('career');
- $extraFieldName = $this->extraFieldIdNameList['career'];
- $externalEventId = null;
- $extraField = new ExtraField('career');
- $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
- $extraFieldName
- );
- if (empty($extraFieldInfo)) {
- return false;
- }
- foreach ($data as $row) {
- foreach ($row as $key => $value) {
- $key = (string) trim($key);
- // Remove utf8 bom
- $key = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $key);
- $row[$key] = $value;
- }
- $itemId = $row['CareerId'];
- $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
- $extraFieldName,
- $itemId,
- false,
- false,
- false
- );
- $career = new Career();
- if (empty($item)) {
- $params = [
- 'status' => 1,
- 'name' => $row['CareerName'],
- ];
- $careerId = $career->save($params);
- if ($careerId) {
- $params = [
- 'item_id' => $careerId,
- 'extra_'.$extraFieldName => $itemId,
- ];
- $links = isset($row['HLinks']) ? $row['HLinks'] : [];
- if (!empty($links)) {
- $extraFieldUrlName = $this->extraFieldIdNameList['career_urls'];
- $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
- $extraFieldUrlName
- );
- if (!empty($extraFieldInfo)) {
- $params['extra_'.$extraFieldUrlName] = $links;
- }
- }
- $extraFieldValue->saveFieldValues($params);
- }
- } else {
- if (isset($item['item_id'])) {
- $params = [
- 'id' => $item['item_id'],
- 'name' => $row['CareerName'],
- ];
- $career->update($params);
- $links = isset($row['HLinks']) ? $row['HLinks'] : [];
- if (!empty($links)) {
- $extraFieldUrlName = $this->extraFieldIdNameList['career_urls'];
- $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
- $extraFieldUrlName
- );
- if (!empty($extraFieldInfo)) {
- $params = [
- 'item_id' => $item['item_id'],
- 'extra_'.$extraFieldName => $itemId,
- 'extra_'.$extraFieldUrlName => $links,
- ];
- $extraFieldValue->saveFieldValues($params);
- }
- }
- }
- }
- }
- }
- }
- /**
- * @param $file
- * @param bool $moveFile
- * @param array $teacherBackup
- * @param array $groupBackup
- */
- private function importCareersDiagram(
- $file,
- $moveFile = false,
- &$teacherBackup = [],
- &$groupBackup = []
- ) {
- $data = Import::csv_reader($file);
- $extraFieldValue = new ExtraFieldValue('career');
- $extraFieldName = $this->extraFieldIdNameList['career'];
- $externalEventId = null;
- $extraField = new ExtraField('career');
- $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
- $extraFieldName
- );
- $careerDiagramExtraFieldName = $this->extraFieldIdNameList['career_diagram'];
- $extraFieldDiagramInfo = $extraField->get_handler_field_info_by_field_variable(
- $careerDiagramExtraFieldName
- );
- if (empty($extraFieldInfo) || empty($extraFieldDiagramInfo)) {
- return false;
- }
- if (!empty($data)) {
- $this->logger->addInfo(count($data)." records found.");
- $values = [];
- foreach ($data as $row) {
- if (empty($row)) {
- continue;
- }
- foreach ($row as $key => $value) {
- $key = (string) trim($key);
- // Remove utf8 bom
- $key = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $key);
- $row[$key] = $value;
- }
- $values[$row['Column']][] = $row;
- }
- $careerList = [];
- $careerNameList = [];
- ksort($values);
- $careerChamiloIdList = [];
- // 1. First create all items
- foreach ($values as $column => $rowList) {
- foreach ($rowList as $row) {
- $careerId = $row['CareerId'];
- $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
- $extraFieldName,
- $careerId,
- false,
- false,
- false
- );
- $chamiloCareerName = '';
- if (empty($item)) {
- $this->logger->addInfo("Career not found: $careerId");
- continue;
- } else {
- if (isset($item['item_id'])) {
- $careerChamiloId = $item['item_id'];
- $career = new Career();
- $career = $career->find($careerChamiloId);
- $chamiloCareerName = $career['name'];
- $careerNameList[$careerId] = $chamiloCareerName;
- $careerChamiloIdList[$careerId] = $careerChamiloId;
- } else {
- continue;
- }
- }
- if (empty($chamiloCareerName)) {
- $this->logger->addInfo("Career not found: $careerId");
- continue;
- }
- if (isset($careerList[$careerId])) {
- $graph = $careerList[$careerId];
- } else {
- $graph = new Graph($careerId);
- $graph->setAttribute('graphviz.graph.rankdir', 'LR');
- $careerList[$careerId] = $graph;
- }
- $currentCourseId = (int) $row['CourseId'];
- $name = $row['CourseName'];
- $notes = $row['Notes'];
- $groupValue = $row['Group'];
- $boxColumn = $row['Column'];
- $rowValue = $row['Row'];
- $color = isset($row['DefinedColor']) ? $row['DefinedColor'] : '';
- $arrow = isset($row['DrawArrowFrom']) ? $row['DrawArrowFrom'] : '';
- $subGroup = isset($row['SubGroup']) ? $row['SubGroup'] : '';
- $connections = isset($row['Connections']) ? $row['Connections'] : '';
- $linkedElement = isset($row['LinkedElement']) ? $row['LinkedElement'] : '';
- if ($graph->hasVertex($currentCourseId)) {
- // Avoid double insertion
- continue;
- } else {
- $current = $graph->createVertex($currentCourseId);
- $current->setAttribute('graphviz.label', $name);
- $current->setAttribute('DefinedColor', $color);
- $current->setAttribute('Notes', $notes);
- $current->setAttribute('Row', $rowValue);
- $current->setAttribute('Group', $groupValue);
- $current->setAttribute('Column', $boxColumn);
- $current->setAttribute('DrawArrowFrom', $arrow);
- $current->setAttribute('SubGroup', $subGroup);
- $current->setAttribute('Connections', $connections);
- $current->setAttribute('LinkedElement', $linkedElement);
- $current->setAttribute('graphviz.shape', 'box');
- $current->setGroup($column);
- }
- }
- }
- // 2. Create connections
- // $column start with 1 (depending in Column row)
- foreach ($values as $column => $rowList) {
- foreach ($rowList as $row) {
- $careerId = $row['CareerId'];
- if (isset($careerList[$careerId])) {
- $graph = $careerList[$careerId];
- } else {
- continue;
- }
- $currentCourseId = (int) $row['CourseId'];
- if ($graph->hasVertex($currentCourseId)) {
- $current = $graph->getVertex($currentCourseId);
- } else {
- continue;
- }
- if (isset($row['DependedOn']) && !empty($row['DependedOn'])) {
- $parentList = explode(',', $row['DependedOn']);
- foreach ($parentList as $parentId) {
- $parentId = (int) $parentId;
- if ($graph->hasVertex($parentId)) {
- /** @var Vertex $parent */
- $parent = $graph->getVertex($parentId);
- /*$parent->setAttribute('graphviz.color', 'red');
- $parent->setAttribute('graphviz.label', $name);
- $parent->setAttribute('graphviz.shape', 'square');*/
- $parent->createEdgeTo($current);
- }
- }
- }
- }
- }
- /** @var Graph $graph */
- foreach ($careerList as $id => $graph) {
- if (isset($careerChamiloIdList[$id])) {
- $params = [
- 'item_id' => $careerChamiloIdList[$id],
- 'extra_'.$careerDiagramExtraFieldName => serialize($graph),
- 'extra_'.$extraFieldName => $id,
- ];
- $extraFieldValue->saveFieldValues($params, true);
- }
- }
- }
- }
- /**
- * @param string $file
- * @param bool $moveFile
- * @param array $teacherBackup
- * @param array $groupBackup
- */
- private function importUnsubscribeStatic(
- $file,
- $moveFile = false,
- &$teacherBackup = [],
- &$groupBackup = []
- ) {
- $data = Import::csv_reader($file);
- if (!empty($data)) {
- $this->logger->addInfo(count($data)." records found.");
- foreach ($data as $row) {
- $chamiloUserName = $row['UserName'];
- $chamiloCourseCode = $row['CourseCode'];
- $chamiloSessionId = $row['SessionID'];
- $sessionInfo = api_get_session_info($chamiloSessionId);
- if (empty($sessionInfo)) {
- $this->logger->addError('Session does not exists: '.$chamiloSessionId);
- continue;
- }
- $courseInfo = api_get_course_info($chamiloCourseCode);
- if (empty($courseInfo)) {
- $this->logger->addError('Course does not exists: '.$courseInfo);
- continue;
- }
- $userId = UserManager::get_user_id_from_username($chamiloUserName);
- if (empty($userId)) {
- $this->logger->addError('User does not exists: '.$chamiloUserName);
- continue;
- }
- $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
- WHERE
- user_id = ".$userId." AND
- c_id = '".$courseInfo['real_id']."'
- ";
- $result = Database::query($sql);
- $rows = Database::num_rows($result);
- if ($rows > 0) {
- $userCourseData = Database::fetch_array($result, 'ASSOC');
- if (!empty($userCourseData)) {
- $teacherBackup[$userId][$courseInfo['code']] = $userCourseData;
- }
- }
- $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)."
- WHERE
- user_id = ".$userId." AND
- c_id = '".$courseInfo['real_id']."'
- ";
- $result = Database::query($sql);
- while ($groupData = Database::fetch_array($result, 'ASSOC')) {
- $groupBackup['user'][$userId][$courseInfo['code']][$groupData['group_id']] = $groupData;
- }
- $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)."
- WHERE
- user_id = ".$userId." AND
- c_id = '".$courseInfo['real_id']."'
- ";
- $result = Database::query($sql);
- while ($groupData = Database::fetch_array($result, 'ASSOC')) {
- $groupBackup['tutor'][$userId][$courseInfo['code']][$groupData['group_id']] = $groupData;
- }
- CourseManager::unsubscribe_user(
- $userId,
- $courseInfo['code'],
- $chamiloSessionId
- );
- $this->logger->addError(
- "User '$chamiloUserName' was removed from session: #$chamiloSessionId, Course: ".$courseInfo['code']
- );
- }
- }
- if ($moveFile) {
- $this->moveFile($file);
- }
- }
- /**
- * Dump database tables.
- */
- private function dumpDatabaseTables()
- {
- echo 'Dumping tables'.PHP_EOL;
- // User
- $table = Database::get_main_table(TABLE_MAIN_USER);
- $tableAdmin = Database::get_main_table(TABLE_MAIN_ADMIN);
- $sql = "DELETE FROM $table
- WHERE user_id not in (select user_id from $tableAdmin) and status <> ".ANONYMOUS;
- Database::query($sql);
- echo $sql.PHP_EOL;
- // Truncate tables
- $truncateTables = [
- Database::get_main_table(TABLE_MAIN_COURSE),
- Database::get_main_table(TABLE_MAIN_COURSE_USER),
- Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE),
- Database::get_main_table(TABLE_MAIN_CATEGORY),
- Database::get_main_table(TABLE_MAIN_COURSE_MODULE),
- Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER),
- Database::get_main_table(TABLE_MAIN_SESSION),
- Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY),
- Database::get_main_table(TABLE_MAIN_SESSION_COURSE),
- Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER),
- Database::get_main_table(TABLE_MAIN_SESSION_USER),
- Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION),
- Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES),
- Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES),
- Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES),
- Database::get_main_table(TABLE_MAIN_USER_FIELD),
- Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS),
- Database::get_main_table(TABLE_MAIN_COURSE_FIELD),
- Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES),
- Database::get_main_table(TABLE_MAIN_SESSION_FIELD),
- Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES),
- Database::get_course_table(TABLE_AGENDA),
- Database::get_course_table(TABLE_AGENDA_ATTACHMENT),
- Database::get_course_table(TABLE_AGENDA_REPEAT),
- Database::get_course_table(TABLE_AGENDA_REPEAT_NOT),
- Database::get_main_table(TABLE_PERSONAL_AGENDA),
- Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT_NOT),
- Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT),
- Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_VALUES),
- Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS),
- Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS),
- Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN),
- Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS),
- Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES),
- Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT),
- Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING),
- Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT),
- Database::get_main_table(TABLE_STATISTIC_TRACK_E_UPLOADS),
- Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT),
- Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY),
- Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY),
- Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION),
- Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG),
- Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT),
- Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT_LOG),
- Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK),
- Database::get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY),
- Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE),
- Database::get_course_table(TABLE_STUDENT_PUBLICATION),
- Database::get_course_table(TABLE_QUIZ_QUESTION),
- Database::get_course_table(TABLE_QUIZ_TEST),
- Database::get_course_table(TABLE_QUIZ_ORDER),
- Database::get_course_table(TABLE_QUIZ_ANSWER),
- Database::get_course_table(TABLE_QUIZ_TEST_QUESTION),
- Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION),
- Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY),
- Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY),
- Database::get_course_table(TABLE_LP_MAIN),
- Database::get_course_table(TABLE_LP_ITEM),
- Database::get_course_table(TABLE_LP_VIEW),
- Database::get_course_table(TABLE_LP_ITEM_VIEW),
- Database::get_course_table(TABLE_DOCUMENT),
- Database::get_course_table(TABLE_ITEM_PROPERTY),
- Database::get_course_table(TABLE_TOOL_LIST),
- Database::get_course_table(TABLE_TOOL_INTRO),
- Database::get_course_table(TABLE_COURSE_SETTING),
- Database::get_course_table(TABLE_SURVEY),
- Database::get_course_table(TABLE_SURVEY_QUESTION),
- Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION),
- Database::get_course_table(TABLE_SURVEY_INVITATION),
- Database::get_course_table(TABLE_SURVEY_ANSWER),
- Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP),
- Database::get_course_table(TABLE_SURVEY_REPORT),
- Database::get_course_table(TABLE_GLOSSARY),
- Database::get_course_table(TABLE_LINK),
- Database::get_course_table(TABLE_LINK_CATEGORY),
- Database::get_course_table(TABLE_GROUP),
- Database::get_course_table(TABLE_GROUP_USER),
- Database::get_course_table(TABLE_GROUP_TUTOR),
- Database::get_course_table(TABLE_GROUP_CATEGORY),
- Database::get_course_table(TABLE_DROPBOX_CATEGORY),
- Database::get_course_table(TABLE_DROPBOX_FEEDBACK),
- Database::get_course_table(TABLE_DROPBOX_POST),
- Database::get_course_table(TABLE_DROPBOX_FILE),
- Database::get_course_table(TABLE_DROPBOX_PERSON),
- ];
- foreach ($truncateTables as $table) {
- $sql = "TRUNCATE $table";
- Database::query($sql);
- echo $sql.PHP_EOL;
- }
- $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $sql = "DELETE FROM $table WHERE tool = 'calendar_event'";
- Database::query($sql);
- echo $sql.PHP_EOL;
- }
- /**
- * If csv file ends with '"' character then a '";' is added.
- *
- * @param string $file
- */
- private function fixCSVFile($file)
- {
- /*$f = fopen($file, 'r+');
- $cursor = -1;
- fseek($f, $cursor, SEEK_END);
- $char = fgetc($f);
- while ($char === "\n" || $char === "\r") {
- fseek($f, $cursor--, SEEK_END);
- $char = fgetc($f);
- }
- if ($char === "\"") {
- fseek($f, -1, SEEK_CUR);
- fwrite($f, '";');
- }*/
- }
- }
- $logger = new Logger('cron');
- $emails = isset($_configuration['cron_notification_mails']) ? $_configuration['cron_notification_mails'] : null;
- $minLevel = Logger::DEBUG;
- if (!is_array($emails)) {
- $emails = [$emails];
- }
- $subject = "Cron main/cron/import_csv.php ".date('Y-m-d h:i:s');
- $from = api_get_setting('emailAdministrator');
- /*
- if (!empty($emails)) {
- foreach ($emails as $email) {
- $stream = new NativeMailerHandler($email, $subject, $from, $minLevel);
- $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
- }
- }*/
- $stream = new StreamHandler(
- api_get_path(SYS_ARCHIVE_PATH).'import_csv.log',
- $minLevel
- );
- $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
- $logger->pushHandler(new RotatingFileHandler('import_csv', 5, $minLevel));
- $cronImportCSVConditions = isset($_configuration['cron_import_csv_conditions']) ? $_configuration['cron_import_csv_conditions'] : null;
- echo 'See the error log here: '.api_get_path(SYS_ARCHIVE_PATH).'import_csv.log'."\n";
- $import = new ImportCsv($logger, $cronImportCSVConditions);
- if (isset($_configuration['default_admin_user_id_for_cron'])) {
- $import->defaultAdminId = $_configuration['default_admin_user_id_for_cron'];
- }
- // @todo in production disable the dump option
- $dump = false;
- if (isset($argv[1]) && $argv[1] = '--dump') {
- $dump = true;
- }
- if (isset($_configuration['import_csv_disable_dump']) &&
- $_configuration['import_csv_disable_dump'] == true
- ) {
- $import->setDumpValues(false);
- } else {
- $import->setDumpValues($dump);
- }
- $import->setUpdateEmailToDummy(api_get_configuration_value('update_users_email_to_dummy_except_admins'));
- // Do not moves the files to treated
- if (isset($_configuration['import_csv_test'])) {
- $import->test = $_configuration['import_csv_test'];
- } else {
- $import->test = true;
- }
- $languageFilesToLoad = api_get_language_files_to_load($import->defaultLanguage);
- foreach ($languageFilesToLoad as $languageFile) {
- include $languageFile;
- }
- // Set default language to be loaded
- $language = $import->defaultLanguage;
- global $language_interface;
- $language_interface = $language;
- global $language_interface_initial_value;
- $language_interface_initial_value = $language;
- $timeStart = microtime(true);
- $import->run();
- $timeEnd = microtime(true);
- $executionTime = round(($timeEnd - $timeStart) / 60, 2);
- $logger->addInfo("Total execution Time $executionTime Min");
- if (isset($_configuration['import_csv_fix_permissions']) &&
- $_configuration['import_csv_fix_permissions'] == true
- ) {
- $command = "sudo find ".api_get_path(SYS_COURSE_PATH)." -type d -exec chmod 777 {} \; ";
- echo "Executing: ".$command.PHP_EOL;
- system($command);
- $command = "sudo find ".api_get_path(SYS_CODE_PATH)."upload/users -type d -exec chmod 777 {} \;";
- echo "Executing: ".$command.PHP_EOL;
- system($command);
- }
|