123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860 |
- <?php
- /* See license terms in /license.txt */
- //use Chamilo\UserBundle\Entity\User;
- /**
- * Class Event
- * Functions of this library are used to record informations when some kind
- * of event occur. Each event has his own types of informations then each event
- * use its own function.
- */
- class Event
- {
- /**
- * @author Sebastien Piraux <piraux_seb@hotmail.com>
- * @desc Record information for open event (when homepage is opened)
- */
- public static function event_open()
- {
- global $TABLETRACK_OPEN;
- // @getHostByAddr($_SERVER['REMOTE_ADDR']) : will provide host and country information
- // $_SERVER['HTTP_USER_AGENT'] : will provide browser and os information
- // $_SERVER['HTTP_REFERER'] : provide information about refering url
- if (isset($_SERVER['HTT_REFERER'])) {
- $referer = Database::escape_string($_SERVER['HTTP_REFERER']);
- } else {
- $referer = '';
- }
- // record information only if user comes from another site
- $pos = strpos($referer, api_get_path(WEB_PATH));
- if ($pos === false && $referer != '') {
- $ip = api_get_real_ip();
- $remhost = @ getHostByAddr($ip);
- if ($remhost == $ip) {
- $remhost = "Unknown";
- } // don't change this
- $reallyNow = api_get_utc_datetime();
- $params = [
- 'open_remote_host' => $remhost,
- 'open_agent' => $_SERVER['HTTP_USER_AGENT'],
- 'open_referer' => $referer,
- 'open_date' => $reallyNow,
- ];
- Database::insert($TABLETRACK_OPEN, $params);
- }
- return 1;
- }
- /**
- * @author Sebastien Piraux <piraux_seb@hotmail.com> old code
- * @author Julio Montoya 2013
- * @desc Record information for login event when an user identifies himself with username & password
- */
- public static function event_login($userId)
- {
- $userInfo = api_get_user_info($userId);
- $userId = intval($userId);
- if (empty($userInfo)) {
- return false;
- }
- $TABLETRACK_LOGIN = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
- $reallyNow = api_get_utc_datetime();
- $sql = "INSERT INTO ".$TABLETRACK_LOGIN." (login_user_id, user_ip, login_date, logout_date) VALUES
- ('".$userId."',
- '".Database::escape_string(api_get_real_ip())."',
- '".$reallyNow."',
- '".$reallyNow."'
- )";
- Database::query($sql);
- // Auto subscribe
- $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::subscribe_user($userId, $code);
- }
- }
- }
- }
- /**
- * @param tool name of the tool (name in mainDb.accueil table)
- * @author Sebastien Piraux <piraux_seb@hotmail.com>
- * @desc Record information for access event for courses
- */
- public static function accessCourse()
- {
- $TABLETRACK_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
- //for "what's new" notification
- $TABLETRACK_LASTACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
- $id_session = api_get_session_id();
- $now = api_get_utc_datetime();
- $courseId = api_get_course_int_id();
- $user_id = api_get_user_id();
- $ip = api_get_real_ip();
- if ($user_id) {
- $user_id = "'".$user_id."'";
- } else {
- $user_id = "0"; // no one
- }
- $sql = "INSERT INTO ".$TABLETRACK_ACCESS." (user_ip, access_user_id, c_id, access_date, access_session_id) VALUES
- ('".$ip."', ".$user_id.", '".$courseId."', '".$now."','".$id_session."')";
- Database::query($sql);
- // added for "what's new" notification
- $sql = "UPDATE $TABLETRACK_LASTACCESS SET access_date = '$now'
- WHERE access_user_id = $user_id AND c_id = '$courseId' AND access_tool IS NULL AND access_session_id=".$id_session;
- $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 (".$user_id.", '".$courseId."', '$now', '".$id_session."')";
- Database::query($sql);
- }
- return 1;
- }
- /**
- * @param string $tool of the tool
- * @author Sebastien Piraux <piraux_seb@hotmail.com>
- * @desc Record information for access event for tools
- *
- * $tool can take this values :
- * Links, Calendar, Document, Announcements,
- * Group, Video, Works, Users, Exercices, Course Desc
- * ...
- * Values can be added if new modules are created (15char max)
- * I encourage to use $nameTool as $tool when calling this function
- *
- * Functionality for "what's new" notification is added by Toon Van Hoecke
- */
- public static function event_access_tool($tool)
- {
- if (empty($tool)) {
- return false;
- }
- $TABLETRACK_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
- //for "what's new" notification
- $TABLETRACK_LASTACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
- $_course = api_get_course_info();
- $courseId = api_get_course_int_id();
- $id_session = api_get_session_id();
- $tool = Database::escape_string($tool);
- $reallyNow = api_get_utc_datetime();
- $user_id = api_get_user_id();
- // record information
- // only if user comes from the course $_cid
- //if( eregi($_configuration['root_web'].$_cid,$_SERVER['HTTP_REFERER'] ) )
- //$pos = strpos($_SERVER['HTTP_REFERER'],$_configuration['root_web'].$_cid);
- $coursePath = isset($_course['path']) ? $_course['path'] : null;
- $pos = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_COURSE_PATH).$coursePath)) : false;
- // added for "what's new" notification
- $pos2 = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_PATH)."index")) : false;
- // end "what's new" notification
- if ($pos !== false || $pos2 !== false) {
- $params = [
- 'access_user_id' => $user_id,
- 'c_id' => $courseId,
- 'access_tool' => $tool,
- 'access_date' => $reallyNow,
- 'access_session_id' => $id_session,
- 'user_ip' => api_get_real_ip()
- ];
- Database::insert($TABLETRACK_ACCESS, $params);
- }
- // "what's new" notification
- $sql = "UPDATE $TABLETRACK_LASTACCESS
- SET access_date = '$reallyNow'
- WHERE access_user_id = ".$user_id." AND c_id = '".$courseId."' AND access_tool = '".$tool."' AND access_session_id=".$id_session;
- $result = Database::query($sql);
- if (Database::affected_rows($result) == 0) {
- $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id, c_id, access_tool, access_date, access_session_id)
- VALUES (".$user_id.", '".$courseId."' , '$tool', '$reallyNow', $id_session)";
- Database::query($sql);
- }
- return 1;
- }
- /**
- * @param doc_id id of document (id in mainDb.document table)
- * @author Sebastien Piraux <piraux_seb@hotmail.com>
- * @desc Record information for download event
- * (when an user click to d/l a document)
- * it will be used in a redirection page
- * bug fixed: Roan Embrechts
- * Roan:
- * The user id is put in single quotes,
- * (why? perhaps to prevent sql insertion hacks?)
- * and later again.
- * Doing this twice causes an error, I remove one of them.
- */
- public static function event_download($doc_url)
- {
- $tbl_stats_downloads = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
- $doc_url = Database::escape_string($doc_url);
- $reallyNow = api_get_utc_datetime();
- $user_id = "'".api_get_user_id()."'";
- $_cid = api_get_course_int_id();
- $sql = "INSERT INTO $tbl_stats_downloads (
- down_user_id,
- c_id,
- down_doc_path,
- down_date,
- down_session_id
- )
- VALUES (
- ".$user_id.",
- '".$_cid."',
- '".$doc_url."',
- '".$reallyNow."',
- '".api_get_session_id()."'
- )";
- Database::query($sql);
- return 1;
- }
- /**
- * @param doc_id id of document (id in mainDb.document table)
- * @author Sebastien Piraux <piraux_seb@hotmail.com>
- * @desc Record information for upload event
- * used in the works tool to record informations when
- * an user upload 1 work
- */
- public static function event_upload($doc_id)
- {
- $TABLETRACK_UPLOADS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_UPLOADS);
- $courseId = api_get_course_int_id();
- $reallyNow = api_get_utc_datetime();
- $user_id = api_get_user_id();
- $doc_id = intval($doc_id);
- $sql = "INSERT INTO ".$TABLETRACK_UPLOADS."
- ( upload_user_id,
- c_id,
- upload_cours_id,
- upload_work_id,
- upload_date,
- upload_session_id
- )
- VALUES (
- ".$user_id.",
- '".$courseId."',
- '',
- '".$doc_id."',
- '".$reallyNow."',
- '".api_get_session_id()."'
- )";
- Database::query($sql);
- return 1;
- }
- /**
- * @param link_id (id in coursDb liens table)
- * @author Sebastien Piraux <piraux_seb@hotmail.com>
- * @desc Record information for link event (when an user click on an added link)
- * it will be used in a redirection page
- */
- public static function event_link($link_id)
- {
- $TABLETRACK_LINKS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LINKS);
- $reallyNow = api_get_utc_datetime();
- $user_id = api_get_user_id();
- $sql = "INSERT INTO ".$TABLETRACK_LINKS."
- ( links_user_id,
- c_id,
- links_link_id,
- links_date,
- links_session_id
- ) VALUES (
- ".$user_id.",
- '".api_get_course_int_id()."',
- '".Database::escape_string($link_id)."',
- '".$reallyNow."',
- '".api_get_session_id()."'
- )";
- Database::query($sql);
- return 1;
- }
- /**
- * Update the TRACK_E_EXERCICES exercises
- *
- * @param int exeid id of the attempt
- * @param int exo_id exercise id
- * @param mixed result score
- * @param int weighting ( higher score )
- * @param int duration ( duration of the attempt in seconds )
- * @param int session_id
- * @param int learnpath_id (id of the learnpath)
- * @param int learnpath_item_id (id of the learnpath_item)
- *
- * @author Sebastien Piraux <piraux_seb@hotmail.com>
- * @author Julio Montoya Armas <gugli100@gmail.com> Reworked 2010
- * @desc Record result of user when an exercice was done
- */
- public static function update_event_exercice(
- $exeid,
- $exo_id,
- $score,
- $weighting,
- $session_id,
- $learnpath_id = 0,
- $learnpath_item_id = 0,
- $learnpath_item_view_id = 0,
- $duration = 0,
- $question_list = array(),
- $status = '',
- $remind_list = array(),
- $end_date = null
- ) {
- global $debug;
- if ($debug) error_log('Called to update_event_exercice');
- if ($debug) error_log('duration:' . $duration);
- if ($exeid != '') {
- /*
- * Code commented due BT#8423 do not change the score to 0.
- *
- * Validation in case of fraud with actived control time
- if (!ExerciseLib::exercise_time_control_is_valid($exo_id, $learnpath_id, $learnpath_item_id)) {
- $score = 0;
- }
- */
- if (!isset($status) || empty($status)) {
- $status = '';
- } else {
- $status = Database::escape_string($status);
- }
- $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- if (!empty($question_list)) {
- $question_list = array_map('intval', $question_list);
- }
- if (!empty($remind_list)) {
- $remind_list = array_map('intval', $remind_list);
- $remind_list = array_filter($remind_list);
- $remind_list = implode(",", $remind_list);
- } else {
- $remind_list = '';
- }
- if (empty($end_date)) {
- $end_date = api_get_utc_datetime();
- }
- $sql = "UPDATE $TABLETRACK_EXERCICES SET
- exe_exo_id = '".Database::escape_string($exo_id)."',
- exe_result = '".Database::escape_string($score)."',
- exe_weighting = '".Database::escape_string($weighting)."',
- session_id = '".Database::escape_string($session_id)."',
- orig_lp_id = '".Database::escape_string($learnpath_id)."',
- orig_lp_item_id = '".Database::escape_string($learnpath_item_id)."',
- orig_lp_item_view_id = '".Database::escape_string($learnpath_item_view_id)."',
- exe_duration = '".Database::escape_string($duration)."',
- exe_date = '".$end_date."',
- status = '".$status."',
- questions_to_check = '".$remind_list."',
- data_tracking = '".implode(',', $question_list)."',
- user_ip = '" . Database::escape_string(api_get_real_ip()) . "'
- WHERE exe_id = '".Database::escape_string($exeid)."'";
- $res = Database::query($sql);
- if ($debug) error_log('update_event_exercice called');
- if ($debug) error_log("$sql");
- //Deleting control time session track
- //ExerciseLib::exercise_time_control_delete($exo_id);
- return $res;
- } else {
- return false;
- }
- }
- /**
- * Record an event for this attempt at answering an exercise
- * @param float Score achieved
- * @param string Answer given
- * @param integer Question ID
- * @param integer Exercise attempt ID a.k.a exe_id (from track_e_exercise)
- * @param integer Position
- * @param integer Exercise ID (from c_quiz)
- * @param bool update results?
- * @param $fileName string Filename (for audio answers - using nanogong)
- * @param integer User ID The user who's going to get this score. Default value of null means "get from context".
- * @param integer Course ID (from the "id" column of course table). Default value of null means "get from context".
- * @param integer Session ID (from the session table). Default value of null means "get from context".
- * @param integer Learnpath ID (from c_lp table). Default value of null means "get from context".
- * @param integer Learnpath item ID (from the c_lp_item table). Default value of null means "get from context".
- * @return boolean Result of the insert query
- */
- 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();
- // check user_id or get from context
- if (empty($user_id)) {
- $user_id = api_get_user_id();
- // anonymous
- if (empty($user_id)) {
- $user_id = api_get_anonymous_id();
- }
- }
- // check course_id or get from context
- if (empty($course_id) or intval($course_id) != $course_id) {
- $course_id = api_get_course_int_id();
- }
- // check session_id or get from context
- if (empty($session_id)) {
- $session_id = api_get_session_id();
- }
- // check learnpath_id or get from context
- if (empty($learnpath_id)) {
- global $learnpath_id;
- }
- // check learnpath_item_id or get from context
- 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");
- }
- //Validation in case of fraud with active control time
- 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;
- }
- $session_id = api_get_session_id();
- if (!empty($question_id) && !empty($exe_id) && !empty($user_id)) {
- $attempt = array(
- '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' => '',
- 'course_code' => ''
- );
- // Check if attempt exists.
- $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) {
- //The attempt already exist do not update use update_event_exercise() instead
- 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 (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
- if ($debug) {
- error_log("Saving e attempt recording ");
- }
- $attempt_recording = array(
- '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,
- array('exe_id = ? AND question_id = ? AND user_id = ? ' => array($exe_id, $question_id, $user_id)));
- if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
- $attempt_recording = array(
- 'exe_id' => $exe_id,
- 'question_id' => $question_id,
- 'marks' => $score,
- 'insert_date' => $now,
- 'author' => '',
- 'session_id' => $session_id,
- );
- Database::update($recording_table, $attempt_recording,
- array('exe_id = ? AND question_id = ? AND session_id = ? ' => array($exe_id, $question_id, $session_id))
- );
- }
- $attempt_id = $exe_id;
- }
- return $attempt_id;
- } else {
- return false;
- }
- }
- /**
- * Record an hotspot spot for this attempt at answering an hotspot question
- * @param int Exercise ID
- * @param int Question ID
- * @param int Answer ID
- * @param int Whether this answer is correct (1) or not (0)
- * @param string Coordinates of this point (e.g. 123;324)
- * @param bool update results?
- * @return boolean Result of the insert query
- * @uses Course code and user_id from global scope $_cid and $_user
- */
- public static function saveExerciseAttemptHotspot(
- $exe_id,
- $question_id,
- $answer_id,
- $correct,
- $coords,
- $updateResults = false,
- $exerciseId = 0
- ) {
- global $safe_lp_id, $safe_lp_item_id;
- $em = Database::getManager();
- if ($updateResults == false) {
- // Validation in case of fraud with activated control time
- if (!ExerciseLib::exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) {
- $correct = 0;
- }
- }
- if ($updateResults) {
- $em
- ->createQuery('
- UPDATE ChamiloCoreBundle:TrackEHotspot teh
- SET teh.hotspotCorrect = :correct,
- teh.hotspotCoordinate = :coords
- WHERE teh.hotspotUserId = :user AND teh.hotspotExeId = :exe AND
- teh.hotspotQuestionId = :question AND teh.hotspotAnswerId = :answer
- ')
- ->execute([
- 'correct' => $correct,
- 'coords' => $coords,
- 'user' => api_get_user_id(),
- 'exe' => $exe_id,
- 'question' => $question_id,
- 'answer' => $answer_id
- ]);
- } else {
- $course = $em->find('ChamiloCoreBundle:', api_get_course_int_id());
- $trackEHotspot = new \Chamilo\CoreBundle\Entity\TrackEHotspot();
- $trackEHotspot
- ->setCourse($course)
- ->setHotspotUserId(api_get_user_id())
- ->setHotspotExeId($exe_id)
- ->setHotspotQuestionId($question_id)
- ->setHotspotAnswerId($answer_id)
- ->setHotspotCorrect($correct)
- ->setHotspotCoordinate($coords);
- $em->persist($trackEHotspot);
- $em->flush();
- return $trackEHotspot->getHotspotId();
- }
- }
- /**
- * Records information for common (or admin) events (in the track_e_default table)
- * @author Yannick Warnier <yannick.warnier@beeznest.com>
- * @param string $event_type Type of event
- * @param string $event_value_type Type of value
- * @param string $event_value Value
- * @param string $datetime Datetime (UTC) (defaults to null)
- * @param int $user_id User ID (defaults to null)
- * @param int $course_id Course ID (defaults to null)
- * @param int $sessionId Session ID
- * @return bool
- * @assert ('','','') === false
- */
- 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 = intval($course_id);
- } else {
- $course_id = api_get_course_int_id();
- }
- if (!empty($sessionId)) {
- $sessionId = intval($sessionId);
- } else {
- $sessionId = api_get_session_id();
- }
- //Clean the user_info
- 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);
- }
- }
- $event_value = Database::escape_string($event_value);
- $sessionId = empty($sessionId) ? api_get_session_id() : intval($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 = array(
- '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;
- }
- /**
- * Get every email stored in the database
- *
- * @param int $etId
- * @return type
- * @assert () !== false
- */
- 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 = array();
- 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;
- }
- /**
- * Get the users related to one event
- *
- * @param string $event_name
- */
- 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);
- }
- /**
- * @param int $user_id
- * @param string $event_type
- * @return array|bool
- */
- public static function get_events_by_user_and_type($user_id, $event_type)
- {
- $TABLETRACK_DEFAULT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
- $user_id = intval($user_id);
- $event_type = Database::escape_string($event_type);
- $sql = "SELECT * FROM $TABLETRACK_DEFAULT
- 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;
- }
- /**
- * Save the new message for one event and for one language
- *
- * @param string $eventName
- * @param array $users
- * @param string $message
- * @param string $subject
- * @param string $eventMessageLanguage
- * @param int $activated
- */
- public static function save_event_type_message($event_name, $users, $message, $subject, $event_message_language, $activated)
- {
- $event_name = Database::escape_string($event_name);
- $activated = intval($activated);
- $event_message_language = Database::escape_string($event_message_language);
- // Deletes then re-adds the users linked to the event
- $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_name = "'.$event_name.'" ';
- Database::query($sql);
- foreach ($users as $user) {
- $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_name)
- VALUES('.intval($user).',"'.$event_name.'")';
- Database::query($sql);
- }
- $language_id = api_get_language_id($event_message_language);
- // check if this template in this language already exists or not
- $sql = 'SELECT COUNT(id) as total FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
- WHERE event_type_name = "'.$event_name.'" AND language_id = '.$language_id;
- $sql = Database::store_result(Database::query($sql), 'ASSOC');
- // if already exists, we update
- if ($sql[0]["total"] > 0) {
- $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
- SET message = "'.Database::escape_string($message).'",
- subject = "'.Database::escape_string($subject).'",
- activated = '.$activated.'
- WHERE event_type_name = "'.$event_name.'" AND language_id = (SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
- WHERE dokeos_folder = "'.$event_message_language.'")';
- Database::query($sql);
- } else { // else we create a new record
- // gets the language_-_id
- $lang_id = '(SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
- WHERE dokeos_folder = "'.$event_message_language.'")';
- $lang_id = Database::store_result(Database::query($lang_id), 'ASSOC');
- if (!empty($lang_id[0]["id"])) {
- $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' (event_type_name, language_id, message, subject, activated)
- VALUES("'.$event_name.'", '.$lang_id[0]["id"].', "'.Database::escape_string($message).'", "'.Database::escape_string($subject).'", '.$activated.')';
- Database::query($sql);
- }
- }
- // set activated at every save
- $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
- SET activated = '.$activated.'
- WHERE event_type_name = "'.$event_name.'"';
- Database::query($sql);
- }
- /**
- * Gets the last attempt of an exercise based in the exe_id
- * @param int $exe_id
- * @return mixed
- */
- public static function getLastAttemptDateOfExercise($exe_id)
- {
- $exe_id = intval($exe_id);
- $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='.$exe_id;
- $rs_last_attempt = Database::query($sql);
- $row_last_attempt = Database::fetch_array($rs_last_attempt);
- $last_attempt_date = $row_last_attempt['last_attempt_date']; //Get the date of last attempt
- return $last_attempt_date;
- }
- /**
- * Gets the last attempt of an exercise based in the exe_id
- * @param int $exe_id
- * @return mixed
- */
- public static function getLatestQuestionIdFromAttempt($exe_id)
- {
- $exe_id = intval($exe_id);
- $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $sql = 'SELECT question_id FROM '.$track_attempts.'
- WHERE exe_id='.$exe_id.'
- 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;
- }
- }
- /**
- * Gets how many attempts exists by user, exercise, learning path
- * @param int user id
- * @param int exercise id
- * @param int lp id
- * @param int lp item id
- * @param int lp item view id
- */
- 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 = intval($user_id);
- $exerciseId = intval($exerciseId);
- $lp_id = intval($lp_id);
- $lp_item_id = intval($lp_item_id);
- $lp_item_view_id = intval($lp_item_view_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 = '".api_get_course_int_id()."' AND
- session_id = '".api_get_session_id()."'";
- $query = Database::query($sql);
- if (Database::num_rows($query) > 0) {
- $attempt = Database :: fetch_array($query, 'ASSOC');
- return $attempt['count'];
- } else {
- return 0;
- }
- }
- /**
- * @param $user_id
- * @param $exerciseId
- * @param $lp_id
- * @param $lp_item_id
- * @return int
- */
- 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 = intval($user_id);
- $exerciseId = intval($exerciseId);
- $lp_id = intval($lp_id);
- $lp_item_id = intval($lp_item_id);
- //$lp_item_view_id = intval($lp_item_view_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 = '".api_get_course_int_id()."' AND
- session_id = '".api_get_session_id()."'";
- $query = Database::query($sql);
- if (Database::num_rows($query) > 0) {
- $attempt = Database :: fetch_array($query, 'ASSOC');
- return $attempt['count'];
- } else {
- return 0;
- }
- }
- /**
- * @param int $user_id
- * @param int $lp_id
- * @param array $course
- * @param int $session_id
- */
- 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);
- $course_id = $course['real_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
- );
- $user_id = intval($user_id);
- $lp_id = intval($lp_id);
- $session_id = intval($session_id);
- //Make sure we have the exact lp_view_id
- $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);
- }
- $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 = array();
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- $exe_list[] = $row['exe_id'];
- }
- if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 0) {
- $sql = "DELETE FROM $track_e_exercises
- WHERE exe_id IN (" . implode(',', $exe_list) . ")";
- Database::query($sql);
- $sql = "DELETE FROM $track_attempts
- WHERE exe_id IN (" . implode(',', $exe_list) . ")";
- Database::query($sql);
- $sql = "DELETE FROM $recording_table
- WHERE exe_id IN (" . implode(',', $exe_list) . ")";
- Database::query($sql);
- }
- Event::addEvent(
- LOG_LP_ATTEMPT_DELETE,
- LOG_LP_ID,
- $lp_id,
- null,
- null,
- $course_id,
- $session_id
- );
- }
- /**
- * Delete all exercise attempts (included in LP or not)
- *
- * @param int user id
- * @param int exercise id
- * @param int $course_id
- * @param int session id
- */
- public static function delete_all_incomplete_attempts($user_id, $exercise_id, $course_id, $session_id = 0)
- {
- $track_e_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $user_id = intval($user_id);
- $exercise_id = intval($exercise_id);
- $course_id = intval($course_id);
- $session_id = intval($session_id);
- if (!empty($user_id) && !empty($exercise_id) && !empty($course_code)) {
- $sql = "DELETE FROM $track_e_exercises
- 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);
- Event::addEvent(
- LOG_EXERCISE_RESULT_DELETE,
- LOG_EXERCISE_AND_USER_ID,
- $exercise_id . '-' . $user_id,
- null,
- null,
- $course_id,
- $session_id
- );
- }
- }
- /**
- * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
- * @param int exercise id
- * @param int $courseId
- * @param int session id
- * @return array with the results
- *
- */
- 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 = intval($courseId);
- $exercise_id = intval($exercise_id);
- $session_id = intval($session_id);
- $user_condition = null;
- if (!empty($user_id)) {
- $user_id = intval($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 = array();
- 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;
- }
- /**
- * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
- * @param int $courseId
- * @param int session id
- * @return array with the results
- *
- */
- 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);
- $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $courseId = intval($courseId);
- $session_id = intval($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 = array();
- 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;
- }
- }
- /**
- * Gets all exercise results (NO Exercises in LPs) from a given exercise id, course, session
- * @param int exercise id
- * @param int $courseId
- * @param int session id
- * @return array with the results
- *
- */
- 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 = intval($courseId);
- $session_id = intval($session_id);
- $user_id = intval($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 = array();
- 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;
- }
- /**
- * Gets exercise results (NO Exercises in LPs) from a given exercise id, course, session
- * @param int exercise id
- * @param string course code
- * @param int session id
- * @return array with the results
- *
- */
- 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 = intval($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 = array();
- if (Database::num_rows($res)) {
- $row = Database::fetch_array($res, 'ASSOC');
- //Checking if this attempt was revised by a teacher
- $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;
- }
- /**
- * Gets exercise results (NO Exercises in LPs) from a given user, exercise id, course, session, lp_id, lp_item_id
- * @param int user id
- * @param int exercise id
- * @param string course code
- * @param int session id
- * @param int lp id
- * @param int lp item id
- * @param string order asc or desc
- * @return array with the results
- *
- */
- 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 = intval($courseId);
- $exercise_id = intval($exercise_id);
- $session_id = intval($session_id);
- $user_id = intval($user_id);
- $lp_id = intval($lp_id);
- $lp_item_id = intval($lp_item_id);
- if (!in_array(strtolower($order), array('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 = array();
- while ($row = Database::fetch_array($res, 'ASSOC')) {
- //Checking if this attempt was revised by a teacher
- $sql = 'SELECT exe_id FROM '.$table_track_attempt_recording.'
- WHERE author != "" AND exe_id = '.$row['exe_id'].'
- 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 = {$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;
- }
- /**
- * Count exercise attempts (NO Exercises in LPs ) from a given exercise id, course, session
- * @param int $user_id
- * @param int exercise id
- * @param int $courseId
- * @param int session id
- * @return array with the results
- *
- */
- public static function count_exercise_attempts_by_user($user_id, $exercise_id, $courseId, $session_id = 0)
- {
- $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $courseId = intval($courseId);
- $exercise_id = intval($exercise_id);
- $session_id = intval($session_id);
- $user_id = intval($user_id);
- $sql = "SELECT count(*) as count FROM $TABLETRACK_EXERCICES
- 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;
- }
- /**
- * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
- * @param int $exercise_id
- * @param int $courseId
- * @param int $session_id
- * @return array with the results
- * @todo rename this function
- */
- public static function get_best_exercise_results_by_user($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 = intval($courseId);
- $exercise_id = intval($exercise_id);
- $session_id = intval($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
- ORDER BY exe_id";
- $res = Database::query($sql);
- $list = array();
- 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;
- }
- }
- // Getting the best results of every student
- $best_score_return = array();
- foreach ($list as $student_result) {
- $user_id = $student_result['exe_user_id'];
- $current_best_score[$user_id] = $student_result['exe_result'];
- //echo $current_best_score[$user_id].' - '.$best_score_return[$user_id]['exe_result'].'<br />';
- 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;
- }
- /**
- * @param int $user_id
- * @param int $exercise_id
- * @param int $courseId
- * @param int $session_id
- * @return array
- */
- 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 = intval($courseId);
- $exercise_id = intval($exercise_id);
- $session_id = intval($session_id);
- $user_id = intval($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 = array();
- 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;
- } */
- }
- //Getting the best results of every student
- $best_score_return = array();
- $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 = array();
- }
- return $best_score_return;
- }
- /**
- * @param int $exercise_id
- * @param int $courseId
- * @param int $session_id
- * @return mixed
- */
- 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 = intval($courseId);
- $session_id = intval($session_id);
- $exercise_id = intval($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'];
- }
- /**
- * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
- * @param int exercise id
- * @param int course id
- * @param int session id
- * @return array with the results
- *
- */
- public static function get_count_exercises_attempted_by_course($courseId, $session_id = 0)
- {
- $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
- $courseId = intval($courseId);
- $session_id = intval($session_id);
- $sql = "SELECT DISTINCT exe_exo_id, exe_user_id
- 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);
- $count = 0;
- if (Database::num_rows($res) > 0) {
- $count = Database::num_rows($res);
- }
- return $count;
- }
- /**
- * Gets all exercise events from a Learning Path within a Course nd Session
- * @param int exercise id
- * @param int $courseId
- * @param int session id
- * @return array
- */
- 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 = intval($courseId);
- $exercise_id = intval($exercise_id);
- $session_id = intval($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 = array();
- 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;
- }
- /**
- * @param int $lp_id
- * @param int $course_id
- *
- * @return array
- */
- public static function get_all_exercises_from_lp($lp_id, $course_id)
- {
- $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM);
- $course_id = intval($course_id);
- $lp_id = intval($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);
- $my_exercise_list = array();
- while ($row = Database::fetch_array($res, 'ASSOC')) {
- $my_exercise_list[] = $row;
- }
- return $my_exercise_list;
- }
- /**
- * This function gets the comments of an exercise
- *
- * @param int $exe_id
- * @param int $question_id
- *
- * @return string the comment
- */
- public static function get_comments($exe_id, $question_id)
- {
- $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
- $sql = "SELECT teacher_comment FROM ".$table_track_attempt."
- WHERE
- exe_id='".Database::escape_string($exe_id)."' AND
- question_id = '".Database::escape_string($question_id)."'
- ORDER by question_id";
- $sqlres = Database::query($sql);
- $comm = Database::result($sqlres, 0, "teacher_comment");
- return $comm;
- }
- /**
- * @param int $exe_id
- *
- * @return array
- */
- public static function getAllExerciseEventByExeId($exe_id)
- {
- $exe_id = intval($exe_id);
- $list = array();
- $em = Database::getManager();
- $qb = $em->createQueryBuilder();
- $qb
- ->select('tea')
- ->from('ChamiloCoreBundle:TrackEAttempt', 'tea')
- ->where(
- $qb->expr()->eq('tea.exeId', $exe_id)
- )
- ->orderBy('tea.position', 'ASC');
- $res_question = $qb->getQuery()->getResult();
- if (count($res_question)) {
- foreach ($res_question as $row) {
- $list[$row->getQuestionId()][] = $row;
- }
- }
- return $list;
- }
- /**
- *
- * @param int $exe_id
- * @param int $user_id
- * @param int $courseId
- * @param int $session_id
- * @param int $question_id
- */
- public static function delete_attempt($exe_id, $user_id, $courseId, $session_id, $question_id)
- {
- $exe_id = intval($exe_id);
- $user_id = intval($user_id);
- $courseId = intval($courseId);
- $session_id = intval($session_id);
- $question_id = intval($question_id);
- $em = Database::getManager();
- $em
- ->createQuery('
- DELETE FROM ChamiloCoreBundle:TrackEAttempt tea
- WHERE tea.exeId = :exe AND tea.userId = :user AND
- tea.course = :course AND tea.sessionId = :session AND tea.questionId = :question
- ')
- ->execute([
- 'exe' => $exe_id,
- 'user' => $user_id,
- 'course' => $courseId,
- 'session' => $session_id,
- 'question' => $question_id
- ]);
- Event::addEvent(
- LOG_QUESTION_RESULT_DELETE,
- LOG_EXERCISE_ATTEMPT_QUESTION_ID,
- $exe_id . '-' . $question_id,
- null,
- null,
- $courseId,
- $session_id
- );
- }
- /**
- * @param $exe_id
- * @param $user_id
- * @param int $courseId
- * @param $question_id
- * @param int $sessionId
- */
- public static function delete_attempt_hotspot($exe_id, $user_id, $courseId, $question_id, $sessionId = null)
- {
- $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
- $exe_id = intval($exe_id);
- $user_id = intval($user_id);
- $courseId = intval($courseId);
- $question_id = intval($question_id);
- if (!isset($sessionId)) {
- $sessionId = api_get_session_id();
- }
- $em = Database::getManager();
- $em
- ->createQuery('
- DELETE FROM ChamiloCoreBundle:TrackEHotspot teh
- WHERE teh.hotspotExeId = :exe AND teh.hotspotUserId = :user AND
- teh.course = :course AND teh.hotspotQuestionId = :question
- ')
- ->execute([
- 'exe' => $exe_id,
- 'user' => $user_id,
- 'course' => $courseId,
- 'question' => $question_id
- ]);
- Event::addEvent(
- LOG_QUESTION_RESULT_DELETE,
- LOG_EXERCISE_ATTEMPT_QUESTION_ID,
- $exe_id . '-' . $question_id,
- null,
- null,
- $courseId,
- $sessionId
- );
- }
- /**
- * User logs in for the first time to a course
- * @param int $courseId
- * @param int $user_id
- * @param int $session_id
- */
- public static function event_course_login($courseId, $user_id, $session_id)
- {
- $course_tracking_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
- $time = api_get_utc_datetime();
- $courseId = intval($courseId);
- $user_id = intval($user_id);
- $session_id = intval($session_id);
- $ip = api_get_real_ip();
- $sql = "INSERT INTO $course_tracking_table(c_id, user_ip, user_id, login_course_date, logout_course_date, counter, session_id)
- VALUES('".$courseId."', '".$ip."', '".$user_id."', '$time', '$time', '1', '".$session_id."')";
- Database::query($sql);
- // Course catalog stats modifications see #4191
- CourseManager::update_course_ranking(null, null, null, null, true, false);
- }
- /**
- * For the sake of genericity, this function is a switch.
- * It's called by EventsDispatcher and fires the good function
- * with the good require_once.
- *
- * @param string $event_name
- * @param array $params
- */
- public static function event_send_mail($event_name, $params)
- {
- EventsMail::send_mail($event_name, $params);
- }
- /**
- * Internal function checking if the mail was already sent from that user to that user
- * @param string $event_name
- * @param int $user_from
- * @param int $user_to
- * @return boolean
- */
- public static function check_if_mail_already_sent($event_name, $user_from, $user_to = null)
- {
- if ($user_to == null) {
- $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
- WHERE user_from = '.$user_from.' AND event_type_name = "'.$event_name.'"';
- } else {
- $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
- WHERE user_from = '.$user_from.' AND user_to = '.$user_to.' AND event_type_name = "'.$event_name.'"';
- }
- $result = Database::store_result(Database::query($sql), 'ASSOC');
- return $result[0]["total"];
- }
- /**
- *
- * Filter EventEmailTemplate Filters see the main/inc/conf/events.conf.dist.php
- *
- */
- /**
- * Basic template event message filter (to be used by other filters as default)
- * @param array $values (passing by reference)
- * @return boolean True if everything is OK, false otherwise
- */
- public function event_send_mail_filter_func(&$values)
- {
- return true;
- }
- /**
- * user_registration - send_mail filter
- * @param array $values (passing by reference)
- * @return boolean True if everything is OK, false otherwise
- */
- public function user_registration_event_send_mail_filter_func(&$values)
- {
- $res = self::event_send_mail_filter_func($values);
- // proper logic for this filter
- return $res;
- }
- /**
- * portal_homepage_edited - send_mail filter
- * @param array $values (passing by reference)
- * @return boolean True if everything is OK, false otherwise
- */
- public function portal_homepage_edited_event_send_mail_filter_func(&$values)
- {
- $res = self::event_send_mail_filter_func($values);
- // proper logic for this filter
- return $res;
- }
- }
|