12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447 |
- <?php
- use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
- use ChamiloSession as Session;
- class Event
- {
-
- public static function open()
- {
- global $_configuration;
- global $TABLETRACK_OPEN;
-
-
-
- if (isset($_SERVER['HTT_REFERER'])) {
- $referer = Database::escape_string($_SERVER['HTTP_REFERER']);
- } else {
- $referer = '';
- }
-
-
- $pos = strpos($referer, $_configuration['root_web']);
- if ($pos === false && $referer != '') {
- $ip = api_get_real_ip();
- $remhost = @gethostbyaddr($ip);
- if ($remhost == $ip) {
- $remhost = "Unknown";
- }
- $reallyNow = api_get_utc_datetime();
- $params = [
- 'open_remote_host' => Database::escape_string($remhost),
- 'open_agent' => $_SERVER['HTTP_USER_AGENT'],
- 'open_referer' => $referer,
- 'open_date' => $reallyNow,
- ];
- Database::insert($TABLETRACK_OPEN, $params);
- }
- return 1;
- }
-
- public static function eventLogin($userId)
- {
- $userInfo = api_get_user_info($userId);
- $userId = (int) $userId;
- if (empty($userInfo)) {
- return false;
- }
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
- $reallyNow = api_get_utc_datetime();
- $userIp = Database::escape_string(api_get_real_ip());
- $sql = "INSERT INTO $table (login_user_id, user_ip, login_date, logout_date) VALUES
- ($userId,
- '$userIp',
- '$reallyNow',
- '$reallyNow'
- )";
- Database::query($sql);
-
- $user_status = $userInfo['status'] == SESSIONADMIN ? 'sessionadmin' : $userInfo['status'] == COURSEMANAGER ? 'teacher' : $userInfo['status'] == DRH ? 'DRH' : 'student';
- $autoSubscribe = api_get_setting($user_status.'_autosubscribe');
- if ($autoSubscribe) {
- $autoSubscribe = explode('|', $autoSubscribe);
- foreach ($autoSubscribe as $code) {
- if (CourseManager::course_exists($code)) {
- CourseManager::subscribeUser($userId, $code);
- }
- }
- }
- return true;
- }
-
- public static function isSessionLogNeedToBeSave($sessionId)
- {
- if (!empty($sessionId)) {
- $visibility = api_get_session_visibility($sessionId);
- if (!empty($visibility) && $visibility != SESSION_AVAILABLE) {
- $extraFieldValue = new ExtraFieldValue('session');
- $value = $extraFieldValue->get_values_by_handler_and_field_variable(
- $sessionId,
- 'disable_log_after_session_ends'
- );
- if (!empty($value) && isset($value['value']) && (int) $value['value'] == 1) {
- return false;
- }
- }
- }
- return true;
- }
-
- public static function accessCourse()
- {
- if (Session::read('login_as')) {
- return false;
- }
- $TABLETRACK_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
-
- $TABLETRACK_LASTACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
- $sessionId = api_get_session_id();
- $now = api_get_utc_datetime();
- $courseId = api_get_course_int_id();
- $userId = api_get_user_id();
- $ip = Database::escape_string(api_get_real_ip());
- if (self::isSessionLogNeedToBeSave($sessionId) === false) {
- return false;
- }
- if ($userId) {
- $userId = $userId;
- } else {
- $userId = '0';
- }
- $sql = "INSERT INTO $TABLETRACK_ACCESS (user_ip, access_user_id, c_id, access_date, access_session_id)
- VALUES ('$ip', $userId, $courseId, '$now', $sessionId)";
- Database::query($sql);
-
- $sql = "UPDATE $TABLETRACK_LASTACCESS SET access_date = '$now'
- WHERE
- access_user_id = $userId AND
- c_id = $courseId AND
- access_tool IS NULL AND
- access_session_id = $sessionId";
- $result = Database::query($sql);
- if (Database::affected_rows($result) == 0) {
- $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id, c_id, access_date, access_session_id)
- VALUES ($userId, $courseId, '$now', $sessionId)";
- Database::query($sql);
- }
- return true;
- }
-
- public static function event_access_tool($tool)
- {
- if (Session::read('login_as')) {
- return false;
- }
- $tool = Database::escape_string($tool);
- if (empty($tool)) {
- return false;
- }
- $courseInfo = api_get_course_info();
- $sessionId = api_get_session_id();
- $reallyNow = api_get_utc_datetime();
- $userId = api_get_user_id();
- if (empty($courseInfo)) {
- return false;
- }
- if (self::isSessionLogNeedToBeSave($sessionId) === false) {
- return false;
- }
- $courseId = $courseInfo['real_id'];
- $tableAccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
-
- $tableLastAccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
-
-
-
-
- $coursePath = isset($courseInfo['path']) ? $courseInfo['path'] : null;
- $pos = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_COURSE_PATH).$coursePath)) : false;
-
- $pos2 = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_PATH)."index")) : false;
-
- if ($pos !== false || $pos2 !== false) {
- $params = [
- 'access_user_id' => $userId,
- 'c_id' => $courseId,
- 'access_tool' => $tool,
- 'access_date' => $reallyNow,
- 'access_session_id' => $sessionId,
- 'user_ip' => Database::escape_string(api_get_real_ip()),
- ];
- Database::insert($tableAccess, $params);
- }
-
- $sql = "UPDATE $tableLastAccess
- SET access_date = '$reallyNow'
- WHERE
- access_user_id = $userId AND
- c_id = $courseId AND
- access_tool = '$tool' AND
- access_session_id = $sessionId";
- $result = Database::query($sql);
- if (Database::affected_rows($result) == 0) {
- $params = [
- 'access_user_id' => $userId,
- 'c_id' => $courseId,
- 'access_tool' => $tool,
- 'access_date' => $reallyNow,
- 'access_session_id' => $sessionId,
- ];
- Database::insert($tableLastAccess, $params);
- }
- return true;
- }
-
- public static function event_download($documentUrl)
- {
- if (Session::read('login_as')) {
- return false;
- }
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
- $documentUrl = Database::escape_string($documentUrl);
- $reallyNow = api_get_utc_datetime();
- $userId = api_get_user_id();
- $courseId = api_get_course_int_id();
- $sessionId = api_get_session_id();
- $sql = "INSERT INTO $table (
- down_user_id,
- c_id,
- down_doc_path,
- down_date,
- down_session_id
- )
- VALUES (
- $userId,
- $courseId,
- '$documentUrl',
- '$reallyNow',
- $sessionId
- )";
- Database::query($sql);
- return 1;
- }
-
- public static function event_upload($documentId)
- {
- if (Session::read('login_as')) {
- return false;
- }
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_UPLOADS);
- $courseId = api_get_course_int_id();
- $reallyNow = api_get_utc_datetime();
- $userId = api_get_user_id();
- $documentId = (int) $documentId;
- $sessionId = api_get_session_id();
- $sql = "INSERT INTO $table
- ( upload_user_id,
- c_id,
- upload_cours_id,
- upload_work_id,
- upload_date,
- upload_session_id
- )
- VALUES (
- $userId,
- $courseId,
- '',
- $documentId,
- '$reallyNow',
- $sessionId
- )";
- Database::query($sql);
- return 1;
- }
-
- public static function event_link($linkId)
- {
- if (Session::read('login_as')) {
- return false;
- }
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LINKS);
- $reallyNow = api_get_utc_datetime();
- $userId = api_get_user_id();
- $courseId = api_get_course_int_id();
- $linkId = (int) $linkId;
- $sessionId = api_get_session_id();
- $sql = "INSERT INTO ".$table."
- ( links_user_id,
- c_id,
- links_link_id,
- links_date,
- links_session_id
- ) VALUES (
- $userId,
- $courseId,
- $linkId,
- '$reallyNow',
- $sessionId
- )";
- Database::query($sql);
- return 1;
- }
-
- public static function updateEventExercise(
- $exeId,
- $exoId,
- $score,
- $weighting,
- $sessionId,
- $learnpathId = 0,
- $learnpathItemId = 0,
- $learnpathItemViewId = 0,
- $duration = 0,
- $questionsList = [],
- $status = '',
- $remindList = [],
- $endDate = null
- ) {
- if (empty($exeId)) {
- return false;
- }
-
- if (!isset($status) || empty($status)) {
- $status = '';
- } else {
- $status = Database::escape_string($status);
- }
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- if (!empty($questionsList)) {
- $questionsList = array_map('intval', $questionsList);
- }
- if (!empty($remindList)) {
- $remindList = array_map('intval', $remindList);
- $remindList = array_filter($remindList);
- $remindList = implode(",", $remindList);
- } else {
- $remindList = '';
- }
- if (empty($endDate)) {
- $endDate = api_get_utc_datetime();
- }
- $exoId = (int) $exoId;
- $sessionId = (int) $sessionId;
- $learnpathId = (int) $learnpathId;
- $learnpathItemId = (int) $learnpathItemId;
- $learnpathItemViewId = (int) $learnpathItemViewId;
- $duration = (int) $duration;
- $exeId = (int) $exeId;
- $score = Database::escape_string($score);
- $weighting = Database::escape_string($weighting);
- $questions = implode(',', $questionsList);
- $userIp = Database::escape_string(api_get_real_ip());
- $sql = "UPDATE $table SET
- exe_exo_id = $exoId,
- exe_result = '$score',
- exe_weighting = '$weighting',
- session_id = $sessionId,
- orig_lp_id = $learnpathId,
- orig_lp_item_id = $learnpathItemId,
- orig_lp_item_view_id = $learnpathItemViewId,
- exe_duration = $duration,
- exe_date = '$endDate',
- status = '$status',
- questions_to_check = '$remindList',
- data_tracking = '$questions',
- user_ip = '$userIp'
- WHERE exe_id = $exeId";
- Database::query($sql);
-
-
- return true;
- }
-
- public static function saveQuestionAttempt(
- $score,
- $answer,
- $question_id,
- $exe_id,
- $position,
- $exercise_id = 0,
- $updateResults = false,
- $fileName = null,
- $user_id = null,
- $course_id = null,
- $session_id = null,
- $learnpath_id = null,
- $learnpath_item_id = null
- ) {
- global $debug;
- $question_id = Database::escape_string($question_id);
- $exe_id = Database::escape_string($exe_id);
- $position = Database::escape_string($position);
- $now = api_get_utc_datetime();
- $course_id = (int) $course_id;
-
- if (empty($user_id)) {
- $user_id = api_get_user_id();
-
- if (empty($user_id)) {
- $user_id = api_get_anonymous_id();
- }
- }
-
- if (empty($course_id)) {
- $course_id = api_get_course_int_id();
- }
-
- $session_id = (int) $session_id;
- if (empty($session_id)) {
- $session_id = api_get_session_id();
- }
-
- if (empty($learnpath_id)) {
- global $learnpath_id;
- }
-
- if (empty($learnpath_item_id)) {
- global $learnpath_item_id;
- }
- $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- if ($debug) {
- error_log("----- entering saveQuestionAttempt() function ------");
- error_log("answer: $answer");
- error_log("score: $score");
- error_log("question_id : $question_id");
- error_log("position: $position");
- }
-
- if (!ExerciseLib::exercise_time_control_is_valid($exercise_id, $learnpath_id, $learnpath_item_id)) {
- if ($debug) {
- error_log("exercise_time_control_is_valid is false");
- }
- $score = 0;
- $answer = 0;
- }
- if (!empty($question_id) && !empty($exe_id) && !empty($user_id)) {
- if (is_null($answer)) {
- $answer = '';
- }
- if (is_null($score)) {
- $score = 0;
- }
- $attempt = [
- 'user_id' => $user_id,
- 'question_id' => $question_id,
- 'answer' => $answer,
- 'marks' => $score,
- 'c_id' => $course_id,
- 'session_id' => $session_id,
- 'position' => $position,
- 'tms' => $now,
- 'filename' => !empty($fileName) ? basename($fileName) : $fileName,
- 'teacher_comment' => '',
- ];
-
- $sql = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT
- WHERE
- c_id = $course_id AND
- session_id = $session_id AND
- exe_id = $exe_id AND
- user_id = $user_id AND
- question_id = $question_id AND
- position = $position";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- if ($debug) {
- error_log("Attempt already exist: exe_id: $exe_id - user_id:$user_id - question_id:$question_id");
- }
- if ($updateResults == false) {
-
- return false;
- }
- } else {
- $attempt['exe_id'] = $exe_id;
- }
- if ($debug) {
- error_log("updateResults : $updateResults");
- error_log("Saving question attempt: ");
- error_log($sql);
- }
- $recording_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
- if ($updateResults == false) {
- $attempt_id = Database::insert($TBL_TRACK_ATTEMPT, $attempt);
- if ($debug) {
- error_log("Insert attempt with id #$attempt_id");
- }
- if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
- if ($debug) {
- error_log("Saving e attempt recording ");
- }
- $attempt_recording = [
- 'exe_id' => $attempt_id,
- 'question_id' => $question_id,
- 'marks' => $score,
- 'insert_date' => $now,
- 'author' => '',
- 'session_id' => $session_id,
- ];
- Database::insert($recording_table, $attempt_recording);
- }
- } else {
- Database::update(
- $TBL_TRACK_ATTEMPT,
- $attempt,
- [
- 'exe_id = ? AND question_id = ? AND user_id = ? ' => [
- $exe_id,
- $question_id,
- $user_id,
- ],
- ]
- );
- if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
- $attempt_recording = [
- 'exe_id' => $exe_id,
- 'question_id' => $question_id,
- 'marks' => $score,
- 'insert_date' => $now,
- 'author' => '',
- 'session_id' => $session_id,
- ];
- Database::update(
- $recording_table,
- $attempt_recording,
- [
- 'exe_id = ? AND question_id = ? AND session_id = ? ' => [
- $exe_id,
- $question_id,
- $session_id,
- ],
- ]
- );
- }
- $attempt_id = $exe_id;
- }
- return $attempt_id;
- } else {
- return false;
- }
- }
-
- public static function saveExerciseAttemptHotspot(
- $exeId,
- $questionId,
- $answerId,
- $correct,
- $coords,
- $updateResults = false,
- $exerciseId = 0
- ) {
- $debug = false;
- global $safe_lp_id, $safe_lp_item_id;
- if ($updateResults == false) {
-
- if (!ExerciseLib::exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) {
- if ($debug) {
- error_log('Attempt is fraud');
- }
- $correct = 0;
- }
- }
- if (empty($exeId)) {
- if ($debug) {
- error_log('exe id is empty');
- }
- return false;
- }
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
- if ($updateResults) {
- if ($debug) {
- error_log("Insert hotspot results: exeId: $exeId correct: $correct");
- }
- $params = [
- 'hotspot_correct' => $correct,
- 'hotspot_coordinate' => $coords,
- ];
- Database::update(
- $table,
- $params,
- [
- 'hotspot_user_id = ? AND hotspot_exe_id = ? AND hotspot_question_id = ? AND hotspot_answer_id = ? ' => [
- api_get_user_id(),
- $exeId,
- $questionId,
- $answerId,
- ],
- ]
- );
- } else {
- if ($debug) {
- error_log("Insert hotspot results: exeId: $exeId correct: $correct");
- }
- return Database::insert(
- $table,
- [
- 'hotspot_course_code' => api_get_course_id(),
- 'hotspot_user_id' => api_get_user_id(),
- 'c_id' => api_get_course_int_id(),
- 'hotspot_exe_id' => $exeId,
- 'hotspot_question_id' => $questionId,
- 'hotspot_answer_id' => $answerId,
- 'hotspot_correct' => $correct,
- 'hotspot_coordinate' => $coords,
- ]
- );
- }
- }
-
- public static function addEvent(
- $event_type,
- $event_value_type,
- $event_value,
- $datetime = null,
- $user_id = null,
- $course_id = null,
- $sessionId = 0
- ) {
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
- if (empty($event_type)) {
- return false;
- }
- $event_type = Database::escape_string($event_type);
- $event_value_type = Database::escape_string($event_value_type);
- if (!empty($course_id)) {
- $course_id = (int) $course_id;
- } else {
- $course_id = api_get_course_int_id();
- }
- if (!empty($sessionId)) {
- $sessionId = (int) $sessionId;
- } else {
- $sessionId = api_get_session_id();
- }
-
- if ($event_value_type == LOG_USER_OBJECT) {
- if (is_array($event_value)) {
- unset($event_value['complete_name']);
- unset($event_value['complete_name_with_username']);
- unset($event_value['firstName']);
- unset($event_value['lastName']);
- unset($event_value['avatar_small']);
- unset($event_value['avatar']);
- unset($event_value['mail']);
- unset($event_value['password']);
- unset($event_value['last_login']);
- unset($event_value['picture_uri']);
- $event_value = serialize($event_value);
- }
- }
-
-
- if (is_array($event_value)) {
- $event_value = serialize($event_value);
- }
- $event_value = Database::escape_string($event_value);
- $sessionId = empty($sessionId) ? api_get_session_id() : (int) $sessionId;
- if (!isset($datetime)) {
- $datetime = api_get_utc_datetime();
- }
- $datetime = Database::escape_string($datetime);
- if (!isset($user_id)) {
- $user_id = api_get_user_id();
- }
- $params = [
- 'default_user_id' => $user_id,
- 'c_id' => $course_id,
- 'default_date' => $datetime,
- 'default_event_type' => $event_type,
- 'default_value_type' => $event_value_type,
- 'default_value' => $event_value,
- 'session_id' => $sessionId,
- ];
- Database::insert($table, $params);
- return true;
- }
-
- public static function get_all_event_types()
- {
- global $event_config;
- $sql = 'SELECT etm.id, event_type_name, activated, language_id, message, subject, dokeos_folder
- FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' etm
- INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l
- ON etm.language_id = l.id';
- $events_types = Database::store_result(Database::query($sql), 'ASSOC');
- $to_return = [];
- foreach ($events_types as $et) {
- $et['nameLangVar'] = $event_config[$et["event_type_name"]]["name_lang_var"];
- $et['descLangVar'] = $event_config[$et["event_type_name"]]["desc_lang_var"];
- $to_return[] = $et;
- }
- return $to_return;
- }
-
- public static function get_event_users($event_name)
- {
- $event_name = Database::escape_string($event_name);
- $sql = 'SELECT user.user_id, user.firstname, user.lastname
- FROM '.Database::get_main_table(TABLE_MAIN_USER).' user
- JOIN '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' relUser
- ON relUser.user_id = user.user_id
- WHERE user.status <> '.ANONYMOUS.' AND relUser.event_type_name = "'.$event_name.'"';
- $user_list = Database::store_result(Database::query($sql), 'ASSOC');
- return json_encode($user_list);
- }
-
- public static function get_events_by_user_and_type($user_id, $event_type)
- {
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
- $user_id = (int) $user_id;
- $event_type = Database::escape_string($event_type);
- $sql = "SELECT * FROM $table
- WHERE default_value_type = 'user_id' AND
- default_value = $user_id AND
- default_event_type = '$event_type'
- ORDER BY default_date ";
- $result = Database::query($sql);
- if ($result) {
- return Database::store_result($result, 'ASSOC');
- }
- return false;
- }
-
- public static function save_event_type_message(
- $event_name,
- $users,
- $message,
- $subject,
- $event_message_language,
- $activated
- ) {
- $event_name = Database::escape_string($event_name);
- $activated = (int) $activated;
- $event_message_language = Database::escape_string($event_message_language);
-
- $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).'
- WHERE event_type_name = "'.$event_name.'" ';
- Database::query($sql);
- $eventTable = Database::get_main_table(TABLE_EVENT_TYPE_REL_USER);
- foreach ($users as $user) {
- $user = (int) $user;
- $sql = "INSERT INTO $eventTable (user_id,event_type_name)
- VALUES($user,'$event_name')";
- Database::query($sql);
- }
- $language_id = api_get_language_id($event_message_language);
-
- $eventMailTable = Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE);
- $sql = "SELECT COUNT(id) as total
- FROM $eventMailTable
- WHERE event_type_name = '$event_name' AND language_id = $language_id";
- $sql = Database::store_result(Database::query($sql), 'ASSOC');
- $languageTable = Database::get_main_table(TABLE_MAIN_LANGUAGE);
- $message = Database::escape_string($message);
- $subject = Database::escape_string($subject);
-
- if ($sql[0]["total"] > 0) {
- $sql = "UPDATE $eventMailTable
- SET message = '$message',
- subject = '$subject',
- activated = $activated
- WHERE event_type_name = '$event_name' AND language_id = (
- SELECT id FROM $languageTable
- WHERE dokeos_folder = '$event_message_language'
- )";
- Database::query($sql);
- } else {
-
- $lang_id = "(SELECT id FROM $languageTable
- WHERE dokeos_folder = '$event_message_language')";
- $lang_id = Database::store_result(Database::query($lang_id), 'ASSOC');
- $lang_id = $lang_id[0]['id'];
- if (!empty($lang_id[0]["id"])) {
- $sql = "INSERT INTO $eventMailTable (event_type_name, language_id, message, subject, activated)
- VALUES ('$event_name', $lang_id, '$message', '$subject', $activated)";
- Database::query($sql);
- }
- }
-
- $sql = "UPDATE $eventMailTable
- SET activated = $activated
- WHERE event_type_name = '$event_name'";
- Database::query($sql);
- }
-
- public static function getLastAttemptDateOfExercise($exeId)
- {
- $exeId = (int) $exeId;
- $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $sql = "SELECT max(tms) as last_attempt_date
- FROM $track_attempts
- WHERE exe_id = $exeId";
- $rs_last_attempt = Database::query($sql);
- $row_last_attempt = Database::fetch_array($rs_last_attempt);
- $date = $row_last_attempt['last_attempt_date'];
- return $date;
- }
-
- public static function getLatestQuestionIdFromAttempt($exeId)
- {
- $exeId = (int) $exeId;
- $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $sql = "SELECT question_id FROM $track_attempts
- WHERE exe_id = $exeId
- ORDER BY tms DESC
- LIMIT 1";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $row = Database::fetch_array($result);
- return $row['question_id'];
- } else {
- return false;
- }
- }
-
- public static function get_attempt_count(
- $user_id,
- $exerciseId,
- $lp_id,
- $lp_item_id,
- $lp_item_view_id
- ) {
- $stat_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $user_id = (int) $user_id;
- $exerciseId = (int) $exerciseId;
- $lp_id = (int) $lp_id;
- $lp_item_id = (int) $lp_item_id;
- $lp_item_view_id = (int) $lp_item_view_id;
- $courseId = api_get_course_int_id();
- $sessionId = api_get_session_id();
- $sql = "SELECT count(*) as count
- FROM $stat_table
- WHERE
- exe_exo_id = $exerciseId AND
- exe_user_id = $user_id AND
- status != 'incomplete' AND
- orig_lp_id = $lp_id AND
- orig_lp_item_id = $lp_item_id AND
- orig_lp_item_view_id = $lp_item_view_id AND
- c_id = $courseId AND
- session_id = $sessionId";
- $query = Database::query($sql);
- if (Database::num_rows($query) > 0) {
- $attempt = Database::fetch_array($query, 'ASSOC');
- return (int) $attempt['count'];
- }
- return 0;
- }
-
- public static function get_attempt_count_not_finished(
- $user_id,
- $exerciseId,
- $lp_id,
- $lp_item_id
- ) {
- $stat_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $user_id = (int) $user_id;
- $exerciseId = (int) $exerciseId;
- $lp_id = (int) $lp_id;
- $lp_item_id = (int) $lp_item_id;
-
- $courseId = api_get_course_int_id();
- $sessionId = api_get_session_id();
- $sql = "SELECT count(*) as count
- FROM $stat_table
- WHERE
- exe_exo_id = $exerciseId AND
- exe_user_id = $user_id AND
- status != 'incomplete' AND
- orig_lp_id = $lp_id AND
- orig_lp_item_id = $lp_item_id AND
- c_id = $courseId AND
- session_id = $sessionId";
- $query = Database::query($sql);
- if (Database::num_rows($query) > 0) {
- $attempt = Database::fetch_array($query, 'ASSOC');
- return $attempt['count'];
- } else {
- return 0;
- }
- }
-
- public static function delete_student_lp_events(
- $user_id,
- $lp_id,
- $course,
- $session_id
- ) {
- $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
- $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
- $lpInteraction = Database::get_course_table(TABLE_LP_IV_INTERACTION);
- $lpObjective = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
- if (empty($course)) {
- return false;
- }
- $course_id = $course['real_id'];
- $user_id = (int) $user_id;
- $lp_id = (int) $lp_id;
- $session_id = (int) $session_id;
- if (empty($course_id)) {
- $course_id = api_get_course_int_id();
- }
- $track_e_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $recording_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
-
- $sql = "SELECT id FROM $lp_view_table
- WHERE
- c_id = $course_id AND
- user_id = $user_id AND
- lp_id = $lp_id AND
- session_id = $session_id";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $view = Database::fetch_array($result, 'ASSOC');
- $lp_view_id = $view['id'];
- $sql = "DELETE FROM $lp_item_view_table
- WHERE c_id = $course_id AND lp_view_id = $lp_view_id";
- Database::query($sql);
- $sql = "DELETE FROM $lpInteraction
- WHERE c_id = $course_id AND lp_iv_id = $lp_view_id";
- Database::query($sql);
- $sql = "DELETE FROM $lpObjective
- WHERE c_id = $course_id AND lp_iv_id = $lp_view_id";
- Database::query($sql);
- }
- if (api_get_configuration_value('lp_minimum_time')) {
- $sql = "DELETE FROM track_e_access_complete
- WHERE
- tool = 'learnpath' AND
- c_id = $course_id AND
- tool_id = $lp_id AND
- user_id = $user_id AND
- session_id = $session_id
- ";
- Database::query($sql);
- }
- $sql = "DELETE FROM $lp_view_table
- WHERE
- c_id = $course_id AND
- user_id = $user_id AND
- lp_id= $lp_id AND
- session_id = $session_id
- ";
- Database::query($sql);
- $sql = "SELECT exe_id FROM $track_e_exercises
- WHERE
- exe_user_id = $user_id AND
- session_id = $session_id AND
- c_id = $course_id AND
- orig_lp_id = $lp_id";
- $result = Database::query($sql);
- $exe_list = [];
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- $exe_list[] = $row['exe_id'];
- }
- if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 0) {
- $exeListString = implode(',', $exe_list);
- $sql = "DELETE FROM $track_e_exercises
- WHERE exe_id IN ($exeListString)";
- Database::query($sql);
- $sql = "DELETE FROM $track_attempts
- WHERE exe_id IN ($exeListString)";
- Database::query($sql);
- $sql = "DELETE FROM $recording_table
- WHERE exe_id IN ($exeListString)";
- Database::query($sql);
- }
- self::addEvent(
- LOG_LP_ATTEMPT_DELETE,
- LOG_LP_ID,
- $lp_id,
- null,
- null,
- $course_id,
- $session_id
- );
- return true;
- }
-
- public static function delete_all_incomplete_attempts(
- $user_id,
- $exercise_id,
- $course_id,
- $session_id = 0
- ) {
- $user_id = (int) $user_id;
- $exercise_id = (int) $exercise_id;
- $course_id = (int) $course_id;
- $session_id = (int) $session_id;
- if (!empty($user_id) && !empty($exercise_id) && !empty($course_id)) {
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $sql = "DELETE FROM $table
- WHERE
- exe_user_id = $user_id AND
- exe_exo_id = $exercise_id AND
- c_id = $course_id AND
- session_id = $session_id AND
- status = 'incomplete' ";
- Database::query($sql);
- self::addEvent(
- LOG_EXERCISE_RESULT_DELETE,
- LOG_EXERCISE_AND_USER_ID,
- $exercise_id.'-'.$user_id,
- null,
- null,
- $course_id,
- $session_id
- );
- }
- }
-
- public static function get_all_exercise_results(
- $exercise_id,
- $courseId,
- $session_id = 0,
- $load_question_list = true,
- $user_id = null
- ) {
- $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $courseId = (int) $courseId;
- $exercise_id = (int) $exercise_id;
- $session_id = (int) $session_id;
- $user_condition = null;
- if (!empty($user_id)) {
- $user_id = (int) $user_id;
- $user_condition = "AND exe_user_id = $user_id ";
- }
- $sql = "SELECT * FROM $TABLETRACK_EXERCICES
- WHERE
- status = '' AND
- c_id = $courseId AND
- exe_exo_id = $exercise_id AND
- session_id = $session_id AND
- orig_lp_id =0 AND
- orig_lp_item_id = 0
- $user_condition
- ORDER BY exe_id";
- $res = Database::query($sql);
- $list = [];
- while ($row = Database::fetch_array($res, 'ASSOC')) {
- $list[$row['exe_id']] = $row;
- if ($load_question_list) {
- $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT
- WHERE exe_id = {$row['exe_id']}";
- $res_question = Database::query($sql);
- while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
- $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
- }
- }
- }
- return $list;
- }
-
- public static function get_all_exercise_results_by_course(
- $courseId,
- $session_id = 0,
- $get_count = true
- ) {
- $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $courseId = (int) $courseId;
- $session_id = (int) $session_id;
- $select = '*';
- if ($get_count) {
- $select = 'count(*) as count';
- }
- $sql = "SELECT $select FROM $table_track_exercises
- WHERE status = '' AND
- c_id = $courseId AND
- session_id = $session_id AND
- orig_lp_id = 0 AND
- orig_lp_item_id = 0
- ORDER BY exe_id";
- $res = Database::query($sql);
- if ($get_count) {
- $row = Database::fetch_array($res, 'ASSOC');
- return $row['count'];
- } else {
- $list = [];
- while ($row = Database::fetch_array($res, 'ASSOC')) {
- $list[$row['exe_id']] = $row;
- }
- return $list;
- }
- }
-
- public static function get_all_exercise_results_by_user(
- $user_id,
- $courseId,
- $session_id = 0
- ) {
- $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $courseId = (int) $courseId;
- $session_id = (int) $session_id;
- $user_id = (int) $user_id;
- $sql = "SELECT * FROM $table_track_exercises
- WHERE
- status = '' AND
- exe_user_id = $user_id AND
- c_id = $courseId AND
- session_id = $session_id AND
- orig_lp_id = 0 AND
- orig_lp_item_id = 0
- ORDER by exe_id";
- $res = Database::query($sql);
- $list = [];
- while ($row = Database::fetch_array($res, 'ASSOC')) {
- $list[$row['exe_id']] = $row;
- $sql = "SELECT * FROM $table_track_attempt
- WHERE exe_id = {$row['exe_id']}";
- $res_question = Database::query($sql);
- while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
- $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
- }
- }
- return $list;
- }
-
- public static function get_exercise_results_by_attempt($exe_id, $status = null)
- {
- $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
- $exe_id = (int) $exe_id;
- $status = Database::escape_string($status);
- $sql = "SELECT * FROM $table_track_exercises
- WHERE status = '$status' AND exe_id = $exe_id";
- $res = Database::query($sql);
- $list = [];
- if (Database::num_rows($res)) {
- $row = Database::fetch_array($res, 'ASSOC');
-
- $sql_revised = "SELECT exe_id FROM $table_track_attempt_recording
- WHERE author != '' AND exe_id = $exe_id
- LIMIT 1";
- $res_revised = Database::query($sql_revised);
- $row['attempt_revised'] = 0;
- if (Database::num_rows($res_revised) > 0) {
- $row['attempt_revised'] = 1;
- }
- $list[$exe_id] = $row;
- $sql = "SELECT * FROM $table_track_attempt
- WHERE exe_id = $exe_id
- ORDER BY tms ASC";
- $res_question = Database::query($sql);
- while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
- $list[$exe_id]['question_list'][$row_q['question_id']] = $row_q;
- }
- }
- return $list;
- }
-
- public static function getExerciseResultsByUser(
- $user_id,
- $exercise_id,
- $courseId,
- $session_id = 0,
- $lp_id = 0,
- $lp_item_id = 0,
- $order = null
- ) {
- $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
- $courseId = (int) $courseId;
- $exercise_id = (int) $exercise_id;
- $session_id = (int) $session_id;
- $user_id = (int) $user_id;
- $lp_id = (int) $lp_id;
- $lp_item_id = (int) $lp_item_id;
- if (!in_array(strtolower($order), ['asc', 'desc'])) {
- $order = 'asc';
- }
- $sql = "SELECT * FROM $table_track_exercises
- WHERE
- status = '' AND
- exe_user_id = $user_id AND
- c_id = $courseId AND
- exe_exo_id = $exercise_id AND
- session_id = $session_id AND
- orig_lp_id = $lp_id AND
- orig_lp_item_id = $lp_item_id
- ORDER by exe_id $order ";
- $res = Database::query($sql);
- $list = [];
- while ($row = Database::fetch_array($res, 'ASSOC')) {
-
- $exeId = $row['exe_id'];
- $sql = "SELECT exe_id FROM $table_track_attempt_recording
- WHERE author != '' AND exe_id = $exeId
- LIMIT 1";
- $res_revised = Database::query($sql);
- $row['attempt_revised'] = 0;
- if (Database::num_rows($res_revised) > 0) {
- $row['attempt_revised'] = 1;
- }
- $list[$row['exe_id']] = $row;
- $sql = "SELECT * FROM $table_track_attempt
- WHERE exe_id = $exeId";
- $res_question = Database::query($sql);
- while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
- $list[$row['exe_id']]['question_list'][$row_q['question_id']][] = $row_q;
- }
- }
- return $list;
- }
-
- public static function count_exercise_attempts_by_user(
- $user_id,
- $exercise_id,
- $courseId,
- $session_id = 0
- ) {
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $courseId = (int) $courseId;
- $exercise_id = (int) $exercise_id;
- $session_id = (int) $session_id;
- $user_id = (int) $user_id;
- $sql = "SELECT count(*) as count
- FROM $table
- WHERE status = '' AND
- exe_user_id = $user_id AND
- c_id = $courseId AND
- exe_exo_id = $exercise_id AND
- session_id = $session_id AND
- orig_lp_id =0 AND
- orig_lp_item_id = 0
- ORDER BY exe_id";
- $res = Database::query($sql);
- $result = 0;
- if (Database::num_rows($res) > 0) {
- $row = Database::fetch_array($res, 'ASSOC');
- $result = $row['count'];
- }
- return $result;
- }
-
- public static function get_best_exercise_results_by_user(
- $exercise_id,
- $courseId,
- $session_id = 0,
- $userId = 0
- ) {
- $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $courseId = (int) $courseId;
- $exercise_id = (int) $exercise_id;
- $session_id = (int) $session_id;
- $sql = "SELECT * FROM $table_track_exercises
- WHERE
- status = '' AND
- c_id = $courseId AND
- exe_exo_id = $exercise_id AND
- session_id = $session_id AND
- orig_lp_id = 0 AND
- orig_lp_item_id = 0";
- if (!empty($userId)) {
- $userId = (int) $userId;
- $sql .= " AND exe_user_id = $userId ";
- }
- $sql .= ' ORDER BY exe_id';
- $res = Database::query($sql);
- $list = [];
- while ($row = Database::fetch_array($res, 'ASSOC')) {
- $list[$row['exe_id']] = $row;
- $exeId = $row['exe_id'];
- $sql = "SELECT * FROM $table_track_attempt
- WHERE exe_id = $exeId";
- $res_question = Database::query($sql);
- while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
- $list[$exeId]['question_list'][$row_q['question_id']] = $row_q;
- }
- }
-
- $best_score_return = [];
- foreach ($list as $student_result) {
- $user_id = $student_result['exe_user_id'];
- $current_best_score[$user_id] = $student_result['exe_result'];
- if (!isset($best_score_return[$user_id]['exe_result'])) {
- $best_score_return[$user_id] = $student_result;
- }
- if ($current_best_score[$user_id] > $best_score_return[$user_id]['exe_result']) {
- $best_score_return[$user_id] = $student_result;
- }
- }
- return $best_score_return;
- }
-
- public static function get_best_attempt_exercise_results_per_user(
- $user_id,
- $exercise_id,
- $courseId,
- $session_id = 0
- ) {
- $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $courseId = (int) $courseId;
- $exercise_id = (int) $exercise_id;
- $session_id = (int) $session_id;
- $user_id = (int) $user_id;
- $sql = "SELECT * FROM $table_track_exercises
- WHERE
- status = '' AND
- c_id = $courseId AND
- exe_exo_id = $exercise_id AND
- session_id = $session_id AND
- exe_user_id = $user_id AND
- orig_lp_id = 0 AND
- orig_lp_item_id = 0
- ORDER BY exe_id";
- $res = Database::query($sql);
- $list = [];
- while ($row = Database::fetch_array($res, 'ASSOC')) {
- $list[$row['exe_id']] = $row;
- }
-
- $best_score_return = [];
- $best_score_return['exe_result'] = 0;
- foreach ($list as $result) {
- $current_best_score = $result;
- if ($current_best_score['exe_result'] > $best_score_return['exe_result']) {
- $best_score_return = $result;
- }
- }
- if (!isset($best_score_return['exe_weighting'])) {
- $best_score_return = [];
- }
- return $best_score_return;
- }
-
- public static function count_exercise_result_not_validated(
- $exercise_id,
- $courseId,
- $session_id = 0
- ) {
- $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
- $courseId = (int) $courseId;
- $session_id = (int) $session_id;
- $exercise_id = (int) $exercise_id;
- $sql = "SELECT count(e.exe_id) as count
- FROM $table_track_exercises e
- LEFT JOIN $table_track_attempt a
- ON e.exe_id = a.exe_id
- WHERE
- exe_exo_id = $exercise_id AND
- c_id = $courseId AND
- e.session_id = $session_id AND
- orig_lp_id = 0 AND
- marks IS NULL AND
- status = '' AND
- orig_lp_item_id = 0
- ORDER BY e.exe_id";
- $res = Database::query($sql);
- $row = Database::fetch_array($res, 'ASSOC');
- return $row['count'];
- }
-
- public static function get_all_exercise_event_from_lp(
- $exercise_id,
- $courseId,
- $session_id = 0
- ) {
- $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $courseId = (int) $courseId;
- $exercise_id = (int) $exercise_id;
- $session_id = (int) $session_id;
- $sql = "SELECT * FROM $table_track_exercises
- WHERE
- status = '' AND
- c_id = $courseId AND
- exe_exo_id = $exercise_id AND
- session_id = $session_id AND
- orig_lp_id !=0 AND
- orig_lp_item_id != 0";
- $res = Database::query($sql);
- $list = [];
- while ($row = Database::fetch_array($res, 'ASSOC')) {
- $exeId = $row['exe_id'];
- $list[$exeId] = $row;
- $sql = "SELECT * FROM $table_track_attempt
- WHERE exe_id = $exeId";
- $res_question = Database::query($sql);
- while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
- $list[$exeId]['question_list'][$row_q['question_id']] = $row_q;
- }
- }
- return $list;
- }
-
- public static function get_all_exercises_from_lp($lp_id, $course_id)
- {
- $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
- $course_id = (int) $course_id;
- $lp_id = (int) $lp_id;
- $sql = "SELECT * FROM $lp_item_table
- WHERE
- c_id = $course_id AND
- lp_id = $lp_id AND
- item_type = 'quiz'
- ORDER BY parent_item_id, display_order";
- $res = Database::query($sql);
- $list = [];
- while ($row = Database::fetch_array($res, 'ASSOC')) {
- $list[] = $row;
- }
- return $list;
- }
-
- public static function get_comments($exe_id, $question_id)
- {
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $exe_id = (int) $exe_id;
- $question_id = (int) $question_id;
- $sql = "SELECT teacher_comment
- FROM $table
- WHERE
- exe_id = $exe_id AND
- question_id = $question_id
- ORDER by question_id";
- $sqlres = Database::query($sql);
- $comm = strval(Database::result($sqlres, 0, 'teacher_comment'));
- $comm = trim($comm);
- return $comm;
- }
-
- public static function getAllExerciseEventByExeId($exeId)
- {
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $exeId = (int) $exeId;
- $sql = "SELECT * FROM $table
- WHERE exe_id = $exeId
- ORDER BY position";
- $res_question = Database::query($sql);
- $list = [];
- if (Database::num_rows($res_question)) {
- while ($row = Database::fetch_array($res_question, 'ASSOC')) {
- $list[$row['question_id']][] = $row;
- }
- }
- return $list;
- }
-
- public static function delete_attempt(
- $exeId,
- $user_id,
- $courseId,
- $session_id,
- $question_id
- ) {
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $exeId = (int) $exeId;
- $user_id = (int) $user_id;
- $courseId = (int) $courseId;
- $session_id = (int) $session_id;
- $question_id = (int) $question_id;
- $sql = "DELETE FROM $table
- WHERE
- exe_id = $exeId AND
- user_id = $user_id AND
- c_id = $courseId AND
- session_id = $session_id AND
- question_id = $question_id ";
- Database::query($sql);
- self::addEvent(
- LOG_QUESTION_RESULT_DELETE,
- LOG_EXERCISE_ATTEMPT_QUESTION_ID,
- $exeId.'-'.$question_id,
- null,
- null,
- $courseId,
- $session_id
- );
- }
-
- public static function delete_attempt_hotspot(
- $exeId,
- $user_id,
- $courseId,
- $question_id,
- $sessionId = null
- ) {
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
- $exeId = (int) $exeId;
- $user_id = (int) $user_id;
- $courseId = (int) $courseId;
- $question_id = (int) $question_id;
- if (!isset($sessionId)) {
- $sessionId = api_get_session_id();
- }
- $sql = "DELETE FROM $table
- WHERE
- hotspot_exe_id = $exeId AND
- hotspot_user_id = $user_id AND
- c_id = $courseId AND
- hotspot_question_id = $question_id ";
- Database::query($sql);
- self::addEvent(
- LOG_QUESTION_RESULT_DELETE,
- LOG_EXERCISE_ATTEMPT_QUESTION_ID,
- $exeId.'-'.$question_id,
- null,
- null,
- $courseId,
- $sessionId
- );
- }
-
- public static function eventCourseLogin($courseId, $user_id, $sessionId)
- {
- if (Session::read('login_as')) {
- return false;
- }
- $sessionId = (int) $sessionId;
- if (self::isSessionLogNeedToBeSave($sessionId) === false) {
- return false;
- }
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
- $loginDate = $logoutDate = api_get_utc_datetime();
-
- $counter = 1;
- $courseId = (int) $courseId;
- $user_id = (int) $user_id;
- $ip = Database::escape_string(api_get_real_ip());
- $sql = "INSERT INTO $table(c_id, user_ip, user_id, login_course_date, logout_course_date, counter, session_id)
- VALUES($courseId, '$ip', $user_id, '$loginDate', '$logoutDate', $counter, $sessionId)";
- $courseAccessId = Database::query($sql);
- if ($courseAccessId) {
-
- CourseManager::update_course_ranking(
- null,
- null,
- null,
- null,
- true,
- false
- );
- return true;
- }
- }
-
- public static function eventCourseLoginUpdate(
- $courseId,
- $userId,
- $sessionId,
- $minutes = 5
- ) {
- if (Session::read('login_as')) {
- return false;
- }
- if (empty($courseId) || empty($userId)) {
- return false;
- }
- $sessionId = (int) $sessionId;
- if (self::isSessionLogNeedToBeSave($sessionId) === false) {
- return false;
- }
- $courseId = (int) $courseId;
- $userId = (int) $userId;
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
- $sql = "SELECT course_access_id, logout_course_date
- FROM $table
- WHERE
- c_id = $courseId AND
- session_id = $sessionId AND
- user_id = $userId
- ORDER BY login_course_date DESC
- LIMIT 1";
- $result = Database::query($sql);
-
- $seconds = $minutes * 60;
- $maxSeconds = 3600;
- if (Database::num_rows($result)) {
- $row = Database::fetch_array($result);
- $id = $row['course_access_id'];
- $logout = $row['logout_course_date'];
- $now = time();
- $logout = api_strtotime($logout, 'UTC');
- if ($now - $logout > $seconds &&
- $now - $logout < $maxSeconds
- ) {
- $now = api_get_utc_datetime();
- $sql = "UPDATE $table SET
- logout_course_date = '$now',
- counter = counter + 1
- WHERE course_access_id = $id";
- Database::query($sql);
- }
- return true;
- }
- return false;
- }
-
- public static function courseLogout($logoutInfo)
- {
- if (Session::read('login_as')) {
- return false;
- }
- if (empty($logoutInfo['uid']) || empty($logoutInfo['cid'])) {
- return false;
- }
- $sessionLifetime = api_get_configuration_value('session_lifetime');
-
- if (empty($sessionLifetime) || $sessionLifetime > 86400) {
- $sessionLifetime = 3600;
- }
- if (!empty($logoutInfo) && !empty($logoutInfo['cid'])) {
- $sessionId = 0;
- if (!empty($logoutInfo['sid'])) {
- $sessionId = (int) $logoutInfo['sid'];
- }
- if (self::isSessionLogNeedToBeSave($sessionId) === false) {
- return false;
- }
- $tableCourseAccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
- $userId = (int) $logoutInfo['uid'];
- $courseId = (int) $logoutInfo['cid'];
- $currentDate = api_get_utc_datetime();
-
- $diff = time() - $sessionLifetime;
- $time = api_get_utc_datetime($diff);
- $sql = "SELECT course_access_id, logout_course_date
- FROM $tableCourseAccess
- WHERE
- user_id = $userId AND
- c_id = $courseId AND
- session_id = $sessionId AND
- login_course_date > '$time'
- ORDER BY login_course_date DESC
- LIMIT 1";
- $result = Database::query($sql);
- $insert = false;
- if (Database::num_rows($result) > 0) {
- $row = Database::fetch_array($result, 'ASSOC');
- $courseAccessId = $row['course_access_id'];
- $sql = "UPDATE $tableCourseAccess SET
- logout_course_date = '$currentDate',
- counter = counter + 1
- WHERE course_access_id = $courseAccessId";
- Database::query($sql);
- } else {
- $insert = true;
- }
- if ($insert) {
- $ip = Database::escape_string(api_get_real_ip());
- $sql = "INSERT INTO $tableCourseAccess (c_id, user_ip, user_id, login_course_date, logout_course_date, counter, session_id)
- VALUES ($courseId, '$ip', $userId, '$currentDate', '$currentDate', 1, $sessionId)";
- Database::query($sql);
- }
- return true;
- }
- }
-
- public static function eventAddVirtualCourseTime(
- $courseId,
- $userId,
- $sessionId,
- $virtualTime = ''
- ) {
- $courseId = (int) $courseId;
- $userId = (int) $userId;
- $sessionId = (int) $sessionId;
- $logoutDate = api_get_utc_datetime();
- $loginDate = ChamiloApi::addOrSubTimeToDateTime(
- $virtualTime,
- $logoutDate,
- false
- );
- $params = [
- 'login_course_date' => $loginDate,
- 'logout_course_date' => $logoutDate,
- 'session_id' => $sessionId,
- 'user_id' => $userId,
- 'counter' => 0,
- 'c_id' => $courseId,
- 'user_ip' => api_get_real_ip(),
- ];
- $courseTrackingTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
- Database::insert($courseTrackingTable, $params);
- return true;
- }
-
- public static function eventRemoveVirtualCourseTime(
- $courseId,
- $userId,
- $sessionId = 0,
- $virtualTime = ''
- ) {
- if (empty($virtualTime)) {
- return false;
- }
- $courseTrackingTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
- $courseId = (int) $courseId;
- $userId = (int) $userId;
- $sessionId = (int) $sessionId;
-
-
-
- if (preg_match('/:/', $virtualTime)) {
- list($h, $m, $s) = preg_split('/:/', $virtualTime);
- $virtualTime = $h * 3600 + $m * 60 + $s;
- } else {
- $virtualTime *= 3600;
- }
-
-
- $sql = "SELECT course_access_id
- FROM $courseTrackingTable
- WHERE
- user_id = $userId AND
- c_id = $courseId AND
- session_id = $sessionId AND
- counter = 0 AND
- (UNIX_TIMESTAMP(logout_course_date) - UNIX_TIMESTAMP(login_course_date)) = '$virtualTime'
- ORDER BY login_course_date DESC LIMIT 0,1";
- $result = Database::query($sql);
-
-
- if (Database::num_rows($result) > 0) {
-
- $row = Database::fetch_row($result);
- $courseAccessId = $row[0];
- $sql = "DELETE FROM $courseTrackingTable
- WHERE course_access_id = $courseAccessId";
- $result = Database::query($sql);
- return $result;
- }
- return false;
- }
-
- public static function event_send_mail($event_name, $params)
- {
- EventsMail::send_mail($event_name, $params);
- }
-
-
- public function event_send_mail_filter_func(&$values)
- {
- return true;
- }
-
- public function user_registration_event_send_mail_filter_func(&$values)
- {
- $res = self::event_send_mail_filter_func($values);
-
- return $res;
- }
-
- public function portal_homepage_edited_event_send_mail_filter_func(&$values)
- {
- $res = self::event_send_mail_filter_func($values);
-
- return $res;
- }
-
- public static function registerLog($logInfo)
- {
- $sessionId = api_get_session_id();
- $courseId = api_get_course_int_id();
- if (!Tracking::minimumTimeAvailable($sessionId, $courseId)) {
- return false;
- }
- if (self::isSessionLogNeedToBeSave($sessionId) === false) {
- return false;
- }
- $loginAs = (int) Session::read('login_as') === true;
- $logInfo['user_id'] = api_get_user_id();
- $logInfo['date_reg'] = api_get_utc_datetime();
- $logInfo['tool'] = !empty($logInfo['tool']) ? $logInfo['tool'] : '';
- $logInfo['tool_id'] = !empty($logInfo['tool_id']) ? (int) $logInfo['tool_id'] : 0;
- $logInfo['tool_id_detail'] = !empty($logInfo['tool_id_detail']) ? (int) $logInfo['tool_id_detail'] : 0;
- $logInfo['action'] = !empty($logInfo['action']) ? $logInfo['action'] : '';
- $logInfo['action_details'] = !empty($logInfo['action_details']) ? $logInfo['action_details'] : '';
- $logInfo['ip_user'] = api_get_real_ip();
- $logInfo['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
- $logInfo['session_id'] = $sessionId;
- $logInfo['c_id'] = $courseId;
- $logInfo['ch_sid'] = session_id();
- $logInfo['login_as'] = $loginAs;
- $logInfo['info'] = !empty($logInfo['info']) ? $logInfo['info'] : '';
- $logInfo['url'] = $_SERVER['REQUEST_URI'];
- $logInfo['current_id'] = Session::read('last_id', 0);
- $id = Database::insert('track_e_access_complete', $logInfo);
- if ($id && empty($logInfo['current_id'])) {
- Session::write('last_id', $id);
- }
- return true;
- }
- }
|