Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Event often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Event, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Event |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @author Sebastien Piraux <[email protected]> |
||
| 17 | * @desc Record information for open event (when homepage is opened) |
||
| 18 | */ |
||
| 19 | public static function event_open() |
||
| 20 | { |
||
| 21 | global $_configuration; |
||
| 22 | global $TABLETRACK_OPEN; |
||
| 23 | |||
| 24 | // @getHostByAddr($_SERVER['REMOTE_ADDR']) : will provide host and country information |
||
| 25 | // $_SERVER['HTTP_USER_AGENT'] : will provide browser and os information |
||
| 26 | // $_SERVER['HTTP_REFERER'] : provide information about refering url |
||
| 27 | if (isset($_SERVER['HTT_REFERER'])) { |
||
| 28 | $referer = Database::escape_string($_SERVER['HTTP_REFERER']); |
||
| 29 | } else { |
||
| 30 | $referer = ''; |
||
| 31 | } |
||
| 32 | // record informations only if user comes from another site |
||
| 33 | //if(!eregi($_configuration['root_web'],$referer)) |
||
| 34 | $pos = strpos($referer, $_configuration['root_web']); |
||
| 35 | if ($pos === false && $referer != '') { |
||
| 36 | $ip = api_get_real_ip(); |
||
| 37 | $remhost = @ getHostByAddr($ip); |
||
| 38 | if ($remhost == $ip) { |
||
| 39 | $remhost = "Unknown"; |
||
| 40 | } // don't change this |
||
| 41 | $reallyNow = api_get_utc_datetime(); |
||
| 42 | $params = [ |
||
| 43 | 'open_remote_host' => $remhost, |
||
| 44 | 'open_agent' => $_SERVER['HTTP_USER_AGENT'], |
||
| 45 | 'open_referer' => $referer, |
||
| 46 | 'open_date' => $reallyNow, |
||
| 47 | ]; |
||
| 48 | Database::insert($TABLETRACK_OPEN, $params); |
||
| 49 | } |
||
| 50 | |||
| 51 | return 1; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @author Sebastien Piraux <[email protected]> old code |
||
| 56 | * @author Julio Montoya 2013 |
||
| 57 | * @desc Record information for login event when an user identifies himself with username & password |
||
| 58 | */ |
||
| 59 | public static function event_login($userId) |
||
| 60 | { |
||
| 61 | $userInfo = api_get_user_info($userId); |
||
| 62 | $userId = intval($userId); |
||
| 63 | |||
| 64 | if (empty($userInfo)) { |
||
| 65 | return false; |
||
| 66 | } |
||
| 67 | |||
| 68 | $TABLETRACK_LOGIN = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN); |
||
| 69 | |||
| 70 | $reallyNow = api_get_utc_datetime(); |
||
| 71 | |||
| 72 | $sql = "INSERT INTO ".$TABLETRACK_LOGIN." (login_user_id, user_ip, login_date, logout_date) VALUES |
||
| 73 | ('".$userId."', |
||
| 74 | '".Database::escape_string(api_get_real_ip())."', |
||
| 75 | '".$reallyNow."', |
||
| 76 | '".$reallyNow."' |
||
| 77 | )"; |
||
| 78 | Database::query($sql); |
||
| 79 | |||
| 80 | // Auto subscribe |
||
| 81 | $user_status = $userInfo['status'] == SESSIONADMIN ? 'sessionadmin' : |
||
| 82 | $userInfo['status'] == COURSEMANAGER ? 'teacher' : |
||
| 83 | $userInfo['status'] == DRH ? 'DRH' : 'student'; |
||
| 84 | $autoSubscribe = api_get_setting($user_status.'_autosubscribe'); |
||
| 85 | if ($autoSubscribe) { |
||
| 86 | $autoSubscribe = explode('|', $autoSubscribe); |
||
| 87 | foreach ($autoSubscribe as $code) { |
||
| 88 | if (CourseManager::course_exists($code)) { |
||
| 89 | CourseManager::subscribe_user($userId, $code); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param tool name of the tool (name in mainDb.accueil table) |
||
| 97 | * @author Sebastien Piraux <[email protected]> |
||
| 98 | * @desc Record information for access event for courses |
||
| 99 | */ |
||
| 100 | public static function accessCourse() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param tool name of the tool (name in mainDb.accueil table) |
||
| 138 | * @author Sebastien Piraux <[email protected]> |
||
| 139 | * @desc Record information for access event for tools |
||
| 140 | * |
||
| 141 | * $tool can take this values : |
||
| 142 | * Links, Calendar, Document, Announcements, |
||
| 143 | * Group, Video, Works, Users, Exercices, Course Desc |
||
| 144 | * ... |
||
| 145 | * Values can be added if new modules are created (15char max) |
||
| 146 | * I encourage to use $nameTool as $tool when calling this function |
||
| 147 | * |
||
| 148 | * Functionality for "what's new" notification is added by Toon Van Hoecke |
||
| 149 | */ |
||
| 150 | public static function event_access_tool($tool, $id_session = 0) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param doc_id id of document (id in mainDb.document table) |
||
| 203 | * @author Sebastien Piraux <[email protected]> |
||
| 204 | * @desc Record information for download event |
||
| 205 | * (when an user click to d/l a document) |
||
| 206 | * it will be used in a redirection page |
||
| 207 | * bug fixed: Roan Embrechts |
||
| 208 | * Roan: |
||
| 209 | * The user id is put in single quotes, |
||
| 210 | * (why? perhaps to prevent sql insertion hacks?) |
||
| 211 | * and later again. |
||
| 212 | * Doing this twice causes an error, I remove one of them. |
||
| 213 | */ |
||
| 214 | View Code Duplication | public static function event_download($doc_url) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @param doc_id id of document (id in mainDb.document table) |
||
| 244 | * @author Sebastien Piraux <[email protected]> |
||
| 245 | * @desc Record information for upload event |
||
| 246 | * used in the works tool to record informations when |
||
| 247 | * an user upload 1 work |
||
| 248 | */ |
||
| 249 | View Code Duplication | public static function event_upload($doc_id) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param link_id (id in coursDb liens table) |
||
| 280 | * @author Sebastien Piraux <[email protected]> |
||
| 281 | * @desc Record information for link event (when an user click on an added link) |
||
| 282 | * it will be used in a redirection page |
||
| 283 | */ |
||
| 284 | public static function event_link($link_id) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Update the TRACK_E_EXERCICES exercises |
||
| 308 | * |
||
| 309 | * @param int exeid id of the attempt |
||
| 310 | * @param int exo_id exercise id |
||
| 311 | * @param mixed result score |
||
| 312 | * @param int weighting ( higher score ) |
||
| 313 | * @param int duration ( duration of the attempt in seconds ) |
||
| 314 | * @param int session_id |
||
| 315 | * @param int learnpath_id (id of the learnpath) |
||
| 316 | * @param int learnpath_item_id (id of the learnpath_item) |
||
| 317 | * |
||
| 318 | * @author Sebastien Piraux <[email protected]> |
||
| 319 | * @author Julio Montoya Armas <[email protected]> Reworked 2010 |
||
| 320 | * @desc Record result of user when an exercise was done |
||
| 321 | */ |
||
| 322 | public static function update_event_exercise( |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Record an event for this attempt at answering an exercise |
||
| 405 | * @param float Score achieved |
||
| 406 | * @param string Answer given |
||
| 407 | * @param integer Question ID |
||
| 408 | * @param integer Exercise attempt ID a.k.a exe_id (from track_e_exercise) |
||
| 409 | * @param integer Position |
||
| 410 | * @param integer Exercise ID (from c_quiz) |
||
| 411 | * @param bool update results? |
||
| 412 | * @param $fileName string Filename (for audio answers - using nanogong) |
||
| 413 | * @param integer User ID The user who's going to get this score. Default value of null means "get from context". |
||
| 414 | * @param integer Course ID (from the "id" column of course table). Default value of null means "get from context". |
||
| 415 | * @param integer Session ID (from the session table). Default value of null means "get from context". |
||
| 416 | * @param integer Learnpath ID (from c_lp table). Default value of null means "get from context". |
||
| 417 | * @param integer Learnpath item ID (from the c_lp_item table). Default value of null means "get from context". |
||
| 418 | * @return boolean Result of the insert query |
||
| 419 | */ |
||
| 420 | public static function saveQuestionAttempt( |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Record an hotspot spot for this attempt at answering an hotspot question |
||
| 589 | * @param int $exeId |
||
| 590 | * @param int $questionId Question ID |
||
| 591 | * @param int $answerId Answer ID |
||
| 592 | * @param int $correct |
||
| 593 | * @param string $coords Coordinates of this point (e.g. 123;324) |
||
| 594 | * @param bool $updateResults |
||
| 595 | * @param int $exerciseId |
||
| 596 | * |
||
| 597 | * @return bool Result of the insert query |
||
| 598 | * @uses Course code and user_id from global scope $_cid and $_user |
||
| 599 | */ |
||
| 600 | public static function saveExerciseAttemptHotspot( |
||
| 601 | $exeId, |
||
| 602 | $questionId, |
||
| 603 | $answerId, |
||
| 604 | $correct, |
||
| 605 | $coords, |
||
| 606 | $updateResults = false, |
||
| 607 | $exerciseId = 0 |
||
| 608 | ) { |
||
| 609 | global $safe_lp_id, $safe_lp_item_id; |
||
| 610 | |||
| 611 | if ($updateResults == false) { |
||
| 612 | // Validation in case of fraud with activated control time |
||
| 613 | if (!ExerciseLib::exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) { |
||
| 614 | $correct = 0; |
||
| 615 | } |
||
| 616 | } |
||
| 617 | |||
| 618 | if (empty($exeId)) { |
||
| 619 | return false; |
||
| 620 | } |
||
| 621 | |||
| 622 | $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT); |
||
| 623 | if ($updateResults) { |
||
| 624 | $params = array( |
||
| 625 | 'hotspot_correct' => $correct, |
||
| 626 | 'hotspot_coordinate' => $coords |
||
| 627 | ); |
||
| 628 | Database::update( |
||
| 629 | $table, |
||
| 630 | $params, |
||
| 631 | array( |
||
| 632 | 'hotspot_user_id = ? AND hotspot_exe_id = ? AND hotspot_question_id = ? AND hotspot_answer_id = ? ' => array( |
||
| 633 | api_get_user_id(), |
||
| 634 | $exeId, |
||
| 635 | $questionId, |
||
| 636 | $answerId |
||
| 637 | ) |
||
| 638 | ) |
||
| 639 | ); |
||
| 640 | |||
| 641 | } else { |
||
| 642 | return Database::insert( |
||
| 643 | $table, |
||
| 644 | [ |
||
| 645 | 'hotspot_course_code' => api_get_course_id(), |
||
| 646 | 'hotspot_user_id' => api_get_user_id(), |
||
| 647 | 'c_id' => api_get_course_int_id(), |
||
| 648 | 'hotspot_exe_id' => $exeId, |
||
| 649 | 'hotspot_question_id' => $questionId, |
||
| 650 | 'hotspot_answer_id' => $answerId, |
||
| 651 | 'hotspot_correct' => $correct, |
||
| 652 | 'hotspot_coordinate' => $coords |
||
| 653 | ] |
||
| 654 | ); |
||
| 655 | } |
||
| 656 | } |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Records information for common (or admin) events (in the track_e_default table) |
||
| 660 | * @author Yannick Warnier <[email protected]> |
||
| 661 | * @param string $event_type Type of event |
||
| 662 | * @param string $event_value_type Type of value |
||
| 663 | * @param string $event_value Value |
||
| 664 | * @param string $datetime Datetime (UTC) (defaults to null) |
||
| 665 | * @param int $user_id User ID (defaults to null) |
||
| 666 | * @param int $course_id Course ID (defaults to null) |
||
| 667 | * @param int $sessionId Session ID |
||
| 668 | * @return bool |
||
| 669 | * @assert ('','','') === false |
||
| 670 | */ |
||
| 671 | public static function addEvent( |
||
| 672 | $event_type, |
||
| 673 | $event_value_type, |
||
| 674 | $event_value, |
||
| 675 | $datetime = null, |
||
| 676 | $user_id = null, |
||
| 677 | $course_id = null, |
||
| 678 | $sessionId = 0 |
||
| 679 | ) { |
||
| 680 | $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT); |
||
| 681 | |||
| 682 | if (empty($event_type)) { |
||
| 683 | return false; |
||
| 684 | } |
||
| 685 | $event_type = Database::escape_string($event_type); |
||
| 686 | $event_value_type = Database::escape_string($event_value_type); |
||
| 687 | if (!empty($course_id)) { |
||
| 688 | $course_id = intval($course_id); |
||
| 689 | } else { |
||
| 690 | $course_id = api_get_course_int_id(); |
||
| 691 | } |
||
| 692 | if (!empty($sessionId)) { |
||
| 693 | $sessionId = intval($sessionId); |
||
| 694 | } else { |
||
| 695 | $sessionId = api_get_session_id(); |
||
| 696 | } |
||
| 697 | |||
| 698 | //Clean the user_info |
||
| 699 | if ($event_value_type == LOG_USER_OBJECT) { |
||
| 700 | if (is_array($event_value)) { |
||
| 701 | unset($event_value['complete_name']); |
||
| 702 | unset($event_value['complete_name_with_username']); |
||
| 703 | unset($event_value['firstName']); |
||
| 704 | unset($event_value['lastName']); |
||
| 705 | unset($event_value['avatar_small']); |
||
| 706 | unset($event_value['avatar']); |
||
| 707 | unset($event_value['mail']); |
||
| 708 | unset($event_value['password']); |
||
| 709 | unset($event_value['last_login']); |
||
| 710 | unset($event_value['picture_uri']); |
||
| 711 | $event_value = serialize($event_value); |
||
| 712 | } |
||
| 713 | } |
||
| 714 | // If event is an array then the $event_value_type should finish with |
||
| 715 | // the suffix _array for example LOG_WORK_DATA = work_data_array |
||
| 716 | if (is_array($event_value)) { |
||
| 717 | $event_value = serialize($event_value); |
||
| 718 | } |
||
| 719 | |||
| 720 | $event_value = Database::escape_string($event_value); |
||
| 721 | $sessionId = empty($sessionId) ? api_get_session_id() : intval($sessionId); |
||
| 722 | |||
| 723 | if (!isset($datetime)) { |
||
| 724 | $datetime = api_get_utc_datetime(); |
||
| 725 | } |
||
| 726 | |||
| 727 | $datetime = Database::escape_string($datetime); |
||
|
|
|||
| 728 | |||
| 729 | if (!isset($user_id)) { |
||
| 730 | $user_id = api_get_user_id(); |
||
| 731 | } |
||
| 732 | |||
| 733 | $params = array( |
||
| 734 | 'default_user_id' => $user_id, |
||
| 735 | 'c_id' => $course_id, |
||
| 736 | 'default_date' => $datetime, |
||
| 737 | 'default_event_type' => $event_type, |
||
| 738 | 'default_value_type' => $event_value_type, |
||
| 739 | 'default_value' => $event_value, |
||
| 740 | 'session_id' => $sessionId |
||
| 741 | ); |
||
| 742 | Database::insert($table, $params); |
||
| 743 | |||
| 744 | return true; |
||
| 745 | } |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Get every email stored in the database |
||
| 749 | * |
||
| 750 | * @return array |
||
| 751 | * @assert () !== false |
||
| 752 | */ |
||
| 753 | public static function get_all_event_types() |
||
| 754 | { |
||
| 755 | global $event_config; |
||
| 756 | |||
| 757 | $sql = 'SELECT etm.id, event_type_name, activated, language_id, message, subject, dokeos_folder |
||
| 758 | FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' etm |
||
| 759 | INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l |
||
| 760 | ON etm.language_id = l.id'; |
||
| 761 | |||
| 762 | $events_types = Database::store_result(Database::query($sql), 'ASSOC'); |
||
| 763 | |||
| 764 | $to_return = array(); |
||
| 765 | foreach ($events_types as $et) { |
||
| 766 | $et['nameLangVar'] = $event_config[$et["event_type_name"]]["name_lang_var"]; |
||
| 767 | $et['descLangVar'] = $event_config[$et["event_type_name"]]["desc_lang_var"]; |
||
| 768 | $to_return[] = $et; |
||
| 769 | } |
||
| 770 | |||
| 771 | return $to_return; |
||
| 772 | } |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Get the users related to one event |
||
| 776 | * |
||
| 777 | * @param string $event_name |
||
| 778 | */ |
||
| 779 | public static function get_event_users($event_name) |
||
| 780 | { |
||
| 781 | $event_name = Database::escape_string($event_name); |
||
| 782 | $sql = 'SELECT user.user_id, user.firstname, user.lastname |
||
| 783 | FROM '.Database::get_main_table(TABLE_MAIN_USER).' user |
||
| 784 | JOIN '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' relUser |
||
| 785 | ON relUser.user_id = user.user_id |
||
| 786 | WHERE user.status <> '.ANONYMOUS.' AND relUser.event_type_name = "'.$event_name.'"'; |
||
| 787 | $user_list = Database::store_result(Database::query($sql), 'ASSOC'); |
||
| 788 | |||
| 789 | return json_encode($user_list); |
||
| 790 | } |
||
| 791 | |||
| 792 | /** |
||
| 793 | * @param int $user_id |
||
| 794 | * @param string $event_type |
||
| 795 | * @return array|bool |
||
| 796 | */ |
||
| 797 | View Code Duplication | public static function get_events_by_user_and_type($user_id, $event_type) |
|
| 798 | { |
||
| 799 | $TABLETRACK_DEFAULT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT); |
||
| 800 | $user_id = intval($user_id); |
||
| 801 | $event_type = Database::escape_string($event_type); |
||
| 802 | |||
| 803 | $sql = "SELECT * FROM $TABLETRACK_DEFAULT |
||
| 804 | WHERE default_value_type = 'user_id' AND |
||
| 805 | default_value = $user_id AND |
||
| 806 | default_event_type = '$event_type' |
||
| 807 | ORDER BY default_date "; |
||
| 808 | $result = Database::query($sql); |
||
| 809 | if ($result) { |
||
| 810 | return Database::store_result($result, 'ASSOC'); |
||
| 811 | } |
||
| 812 | return false; |
||
| 813 | } |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Save the new message for one event and for one language |
||
| 817 | * |
||
| 818 | * @param string $eventName |
||
| 819 | * @param array $users |
||
| 820 | * @param string $message |
||
| 821 | * @param string $subject |
||
| 822 | * @param string $eventMessageLanguage |
||
| 823 | * @param int $activated |
||
| 824 | */ |
||
| 825 | public static function save_event_type_message($event_name, $users, $message, $subject, $event_message_language, $activated) |
||
| 826 | { |
||
| 827 | $event_name = Database::escape_string($event_name); |
||
| 828 | $activated = intval($activated); |
||
| 829 | $event_message_language = Database::escape_string($event_message_language); |
||
| 830 | |||
| 831 | // Deletes then re-adds the users linked to the event |
||
| 832 | $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_name = "'.$event_name.'" '; |
||
| 833 | Database::query($sql); |
||
| 834 | |||
| 835 | foreach ($users as $user) { |
||
| 836 | $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_name) |
||
| 837 | VALUES('.intval($user).',"'.$event_name.'")'; |
||
| 838 | Database::query($sql); |
||
| 839 | } |
||
| 840 | $language_id = api_get_language_id($event_message_language); |
||
| 841 | // check if this template in this language already exists or not |
||
| 842 | $sql = 'SELECT COUNT(id) as total FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' |
||
| 843 | WHERE event_type_name = "'.$event_name.'" AND language_id = '.$language_id; |
||
| 844 | |||
| 845 | $sql = Database::store_result(Database::query($sql), 'ASSOC'); |
||
| 846 | |||
| 847 | // if already exists, we update |
||
| 848 | if ($sql[0]["total"] > 0) { |
||
| 849 | $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' |
||
| 850 | SET message = "'.Database::escape_string($message).'", |
||
| 851 | subject = "'.Database::escape_string($subject).'", |
||
| 852 | activated = '.$activated.' |
||
| 853 | WHERE event_type_name = "'.$event_name.'" AND language_id = (SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' |
||
| 854 | WHERE dokeos_folder = "'.$event_message_language.'")'; |
||
| 855 | Database::query($sql); |
||
| 856 | } else { // else we create a new record |
||
| 857 | // gets the language_-_id |
||
| 858 | $lang_id = '(SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' |
||
| 859 | WHERE dokeos_folder = "'.$event_message_language.'")'; |
||
| 860 | $lang_id = Database::store_result(Database::query($lang_id), 'ASSOC'); |
||
| 861 | |||
| 862 | if (!empty($lang_id[0]["id"])) { |
||
| 863 | $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' (event_type_name, language_id, message, subject, activated) |
||
| 864 | VALUES("'.$event_name.'", '.$lang_id[0]["id"].', "'.Database::escape_string($message).'", "'.Database::escape_string($subject).'", '.$activated.')'; |
||
| 865 | Database::query($sql); |
||
| 866 | } |
||
| 867 | } |
||
| 868 | |||
| 869 | // set activated at every save |
||
| 870 | $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' |
||
| 871 | SET activated = '.$activated.' |
||
| 872 | WHERE event_type_name = "'.$event_name.'"'; |
||
| 873 | Database::query($sql); |
||
| 874 | } |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Gets the last attempt of an exercise based in the exe_id |
||
| 878 | * @param int $exe_id |
||
| 879 | * @return mixed |
||
| 880 | */ |
||
| 881 | public static function getLastAttemptDateOfExercise($exe_id) |
||
| 882 | { |
||
| 883 | $exe_id = intval($exe_id); |
||
| 884 | $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); |
||
| 885 | $sql = 'SELECT max(tms) as last_attempt_date |
||
| 886 | FROM '.$track_attempts.' |
||
| 887 | WHERE exe_id='.$exe_id; |
||
| 888 | $rs_last_attempt = Database::query($sql); |
||
| 889 | $row_last_attempt = Database::fetch_array($rs_last_attempt); |
||
| 890 | $last_attempt_date = $row_last_attempt['last_attempt_date']; //Get the date of last attempt |
||
| 891 | |||
| 892 | return $last_attempt_date; |
||
| 893 | } |
||
| 894 | |||
| 895 | /** |
||
| 896 | * Gets the last attempt of an exercise based in the exe_id |
||
| 897 | * @param int $exe_id |
||
| 898 | * @return mixed |
||
| 899 | */ |
||
| 900 | public static function getLatestQuestionIdFromAttempt($exe_id) |
||
| 901 | { |
||
| 902 | $exe_id = intval($exe_id); |
||
| 903 | $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); |
||
| 904 | $sql = 'SELECT question_id FROM '.$track_attempts.' |
||
| 905 | WHERE exe_id='.$exe_id.' |
||
| 906 | ORDER BY tms DESC |
||
| 907 | LIMIT 1'; |
||
| 908 | $result = Database::query($sql); |
||
| 909 | if (Database::num_rows($result)) { |
||
| 910 | $row = Database::fetch_array($result); |
||
| 911 | return $row['question_id']; |
||
| 912 | } else { |
||
| 913 | return false; |
||
| 914 | } |
||
| 915 | } |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Gets how many attempts exists by user, exercise, learning path |
||
| 919 | * @param int user id |
||
| 920 | * @param int exercise id |
||
| 921 | * @param int lp id |
||
| 922 | * @param int lp item id |
||
| 923 | * @param int lp item view id |
||
| 924 | */ |
||
| 925 | public static function get_attempt_count($user_id, $exerciseId, $lp_id, $lp_item_id, $lp_item_view_id) |
||
| 926 | { |
||
| 927 | $stat_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 928 | $user_id = intval($user_id); |
||
| 929 | $exerciseId = intval($exerciseId); |
||
| 930 | $lp_id = intval($lp_id); |
||
| 931 | $lp_item_id = intval($lp_item_id); |
||
| 932 | $lp_item_view_id = intval($lp_item_view_id); |
||
| 933 | |||
| 934 | $sql = "SELECT count(*) as count |
||
| 935 | FROM $stat_table |
||
| 936 | WHERE |
||
| 937 | exe_exo_id = $exerciseId AND |
||
| 938 | exe_user_id = $user_id AND |
||
| 939 | status != 'incomplete' AND |
||
| 940 | orig_lp_id = $lp_id AND |
||
| 941 | orig_lp_item_id = $lp_item_id AND |
||
| 942 | orig_lp_item_view_id = $lp_item_view_id AND |
||
| 943 | c_id = '".api_get_course_int_id()."' AND |
||
| 944 | session_id = '".api_get_session_id()."'"; |
||
| 945 | |||
| 946 | $query = Database::query($sql); |
||
| 947 | View Code Duplication | if (Database::num_rows($query) > 0) { |
|
| 948 | $attempt = Database::fetch_array($query, 'ASSOC'); |
||
| 949 | return $attempt['count']; |
||
| 950 | } else { |
||
| 951 | return 0; |
||
| 952 | } |
||
| 953 | } |
||
| 954 | |||
| 955 | /** |
||
| 956 | * @param $user_id |
||
| 957 | * @param $exerciseId |
||
| 958 | * @param $lp_id |
||
| 959 | * @param $lp_item_id |
||
| 960 | * @return int |
||
| 961 | */ |
||
| 962 | public static function get_attempt_count_not_finished($user_id, $exerciseId, $lp_id, $lp_item_id) |
||
| 963 | { |
||
| 964 | $stat_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 965 | $user_id = intval($user_id); |
||
| 966 | $exerciseId = intval($exerciseId); |
||
| 967 | $lp_id = intval($lp_id); |
||
| 968 | $lp_item_id = intval($lp_item_id); |
||
| 969 | //$lp_item_view_id = intval($lp_item_view_id); |
||
| 970 | |||
| 971 | $sql = "SELECT count(*) as count |
||
| 972 | FROM $stat_table |
||
| 973 | WHERE |
||
| 974 | exe_exo_id = $exerciseId AND |
||
| 975 | exe_user_id = $user_id AND |
||
| 976 | status != 'incomplete' AND |
||
| 977 | orig_lp_id = $lp_id AND |
||
| 978 | orig_lp_item_id = $lp_item_id AND |
||
| 979 | c_id = '".api_get_course_int_id()."' AND |
||
| 980 | session_id = '".api_get_session_id()."'"; |
||
| 981 | |||
| 982 | $query = Database::query($sql); |
||
| 983 | View Code Duplication | if (Database::num_rows($query) > 0) { |
|
| 984 | $attempt = Database::fetch_array($query, 'ASSOC'); |
||
| 985 | return $attempt['count']; |
||
| 986 | } else { |
||
| 987 | return 0; |
||
| 988 | } |
||
| 989 | } |
||
| 990 | |||
| 991 | /** |
||
| 992 | * @param int $user_id |
||
| 993 | * @param int $lp_id |
||
| 994 | * @param array $course |
||
| 995 | * @param int $session_id |
||
| 996 | */ |
||
| 997 | public static function delete_student_lp_events($user_id, $lp_id, $course, $session_id) |
||
| 998 | { |
||
| 999 | $lp_view_table = Database::get_course_table(TABLE_LP_VIEW); |
||
| 1000 | $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW); |
||
| 1001 | $lpInteraction = Database::get_course_table(TABLE_LP_IV_INTERACTION); |
||
| 1002 | $lpObjective = Database::get_course_table(TABLE_LP_IV_OBJECTIVE); |
||
| 1003 | |||
| 1004 | $course_id = $course['real_id']; |
||
| 1005 | |||
| 1006 | if (empty($course_id)) { |
||
| 1007 | $course_id = api_get_course_int_id(); |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | $track_e_exercises = Database::get_main_table( |
||
| 1011 | TABLE_STATISTIC_TRACK_E_EXERCISES |
||
| 1012 | ); |
||
| 1013 | $track_attempts = Database::get_main_table( |
||
| 1014 | TABLE_STATISTIC_TRACK_E_ATTEMPT |
||
| 1015 | ); |
||
| 1016 | $recording_table = Database::get_main_table( |
||
| 1017 | TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING |
||
| 1018 | ); |
||
| 1019 | |||
| 1020 | $user_id = intval($user_id); |
||
| 1021 | $lp_id = intval($lp_id); |
||
| 1022 | $session_id = intval($session_id); |
||
| 1023 | |||
| 1024 | //Make sure we have the exact lp_view_id |
||
| 1025 | $sql = "SELECT id FROM $lp_view_table |
||
| 1026 | WHERE |
||
| 1027 | c_id = $course_id AND |
||
| 1028 | user_id = $user_id AND |
||
| 1029 | lp_id = $lp_id AND |
||
| 1030 | session_id = $session_id "; |
||
| 1031 | $result = Database::query($sql); |
||
| 1032 | |||
| 1033 | View Code Duplication | if (Database::num_rows($result)) { |
|
| 1034 | $view = Database::fetch_array($result, 'ASSOC'); |
||
| 1035 | $lp_view_id = $view['id']; |
||
| 1036 | |||
| 1037 | $sql = "DELETE FROM $lp_item_view_table |
||
| 1038 | WHERE c_id = $course_id AND lp_view_id = $lp_view_id "; |
||
| 1039 | Database::query($sql); |
||
| 1040 | |||
| 1041 | $sql = "DELETE FROM $lpInteraction |
||
| 1042 | WHERE c_id = $course_id AND lp_iv_id = $lp_view_id "; |
||
| 1043 | Database::query($sql); |
||
| 1044 | |||
| 1045 | $sql = "DELETE FROM $lpObjective |
||
| 1046 | WHERE c_id = $course_id AND lp_iv_id = $lp_view_id "; |
||
| 1047 | Database::query($sql); |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | $sql = "DELETE FROM $lp_view_table |
||
| 1051 | WHERE |
||
| 1052 | c_id = $course_id AND |
||
| 1053 | user_id = $user_id AND |
||
| 1054 | lp_id= $lp_id AND |
||
| 1055 | session_id = $session_id |
||
| 1056 | "; |
||
| 1057 | Database::query($sql); |
||
| 1058 | |||
| 1059 | $sql = "SELECT exe_id FROM $track_e_exercises |
||
| 1060 | WHERE exe_user_id = $user_id AND |
||
| 1061 | session_id = $session_id AND |
||
| 1062 | c_id = $course_id AND |
||
| 1063 | orig_lp_id = $lp_id"; |
||
| 1064 | $result = Database::query($sql); |
||
| 1065 | $exe_list = array(); |
||
| 1066 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 1067 | $exe_list[] = $row['exe_id']; |
||
| 1068 | } |
||
| 1069 | |||
| 1070 | if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 0) { |
||
| 1071 | $sql = "DELETE FROM $track_e_exercises |
||
| 1072 | WHERE exe_id IN (" . implode(',', $exe_list) . ")"; |
||
| 1073 | Database::query($sql); |
||
| 1074 | |||
| 1075 | $sql = "DELETE FROM $track_attempts |
||
| 1076 | WHERE exe_id IN (" . implode(',', $exe_list) . ")"; |
||
| 1077 | Database::query($sql); |
||
| 1078 | |||
| 1079 | $sql = "DELETE FROM $recording_table |
||
| 1080 | WHERE exe_id IN (" . implode(',', $exe_list) . ")"; |
||
| 1081 | Database::query($sql); |
||
| 1082 | } |
||
| 1083 | |||
| 1084 | self::addEvent( |
||
| 1085 | LOG_LP_ATTEMPT_DELETE, |
||
| 1086 | LOG_LP_ID, |
||
| 1087 | $lp_id, |
||
| 1088 | null, |
||
| 1089 | null, |
||
| 1090 | $course_id, |
||
| 1091 | $session_id |
||
| 1092 | ); |
||
| 1093 | } |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * Delete all exercise attempts (included in LP or not) |
||
| 1097 | * |
||
| 1098 | * @param int user id |
||
| 1099 | * @param int exercise id |
||
| 1100 | * @param int $course_id |
||
| 1101 | * @param int session id |
||
| 1102 | */ |
||
| 1103 | public static function delete_all_incomplete_attempts($user_id, $exercise_id, $course_id, $session_id = 0) |
||
| 1104 | { |
||
| 1105 | $track_e_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 1106 | $user_id = intval($user_id); |
||
| 1107 | $exercise_id = intval($exercise_id); |
||
| 1108 | $course_id = intval($course_id); |
||
| 1109 | $session_id = intval($session_id); |
||
| 1110 | if (!empty($user_id) && !empty($exercise_id) && !empty($course_code)) { |
||
| 1111 | $sql = "DELETE FROM $track_e_exercises |
||
| 1112 | WHERE |
||
| 1113 | exe_user_id = $user_id AND |
||
| 1114 | exe_exo_id = $exercise_id AND |
||
| 1115 | c_id = '$course_id' AND |
||
| 1116 | session_id = $session_id AND |
||
| 1117 | status = 'incomplete' "; |
||
| 1118 | Database::query($sql); |
||
| 1119 | self::addEvent( |
||
| 1120 | LOG_EXERCISE_RESULT_DELETE, |
||
| 1121 | LOG_EXERCISE_AND_USER_ID, |
||
| 1122 | $exercise_id . '-' . $user_id, |
||
| 1123 | null, |
||
| 1124 | null, |
||
| 1125 | $course_id, |
||
| 1126 | $session_id |
||
| 1127 | ); |
||
| 1128 | } |
||
| 1129 | } |
||
| 1130 | |||
| 1131 | /** |
||
| 1132 | * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session |
||
| 1133 | * @param int exercise id |
||
| 1134 | * @param int $courseId |
||
| 1135 | * @param int session id |
||
| 1136 | * @return array with the results |
||
| 1137 | * |
||
| 1138 | */ |
||
| 1139 | public static function get_all_exercise_results( |
||
| 1140 | $exercise_id, |
||
| 1141 | $courseId, |
||
| 1142 | $session_id = 0, |
||
| 1143 | $load_question_list = true, |
||
| 1144 | $user_id = null |
||
| 1145 | ) { |
||
| 1146 | $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 1147 | $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); |
||
| 1148 | $courseId = intval($courseId); |
||
| 1149 | $exercise_id = intval($exercise_id); |
||
| 1150 | $session_id = intval($session_id); |
||
| 1151 | |||
| 1152 | $user_condition = null; |
||
| 1153 | if (!empty($user_id)) { |
||
| 1154 | $user_id = intval($user_id); |
||
| 1155 | $user_condition = "AND exe_user_id = $user_id "; |
||
| 1156 | } |
||
| 1157 | $sql = "SELECT * FROM $TABLETRACK_EXERCICES |
||
| 1158 | WHERE |
||
| 1159 | status = '' AND |
||
| 1160 | c_id = '$courseId' AND |
||
| 1161 | exe_exo_id = '$exercise_id' AND |
||
| 1162 | session_id = $session_id AND |
||
| 1163 | orig_lp_id =0 AND |
||
| 1164 | orig_lp_item_id = 0 |
||
| 1165 | $user_condition |
||
| 1166 | ORDER BY exe_id"; |
||
| 1167 | $res = Database::query($sql); |
||
| 1168 | $list = array(); |
||
| 1169 | while ($row = Database::fetch_array($res, 'ASSOC')) { |
||
| 1170 | $list[$row['exe_id']] = $row; |
||
| 1171 | if ($load_question_list) { |
||
| 1172 | $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT |
||
| 1173 | WHERE exe_id = {$row['exe_id']}"; |
||
| 1174 | $res_question = Database::query($sql); |
||
| 1175 | while ($row_q = Database::fetch_array($res_question, 'ASSOC')) { |
||
| 1176 | $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q; |
||
| 1177 | } |
||
| 1178 | } |
||
| 1179 | } |
||
| 1180 | return $list; |
||
| 1181 | } |
||
| 1182 | |||
| 1183 | /** |
||
| 1184 | * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session |
||
| 1185 | * @param int $courseId |
||
| 1186 | * @param int session id |
||
| 1187 | * @return array with the results |
||
| 1188 | * |
||
| 1189 | */ |
||
| 1190 | public static function get_all_exercise_results_by_course($courseId, $session_id = 0, $get_count = true) |
||
| 1191 | { |
||
| 1192 | $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 1193 | $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); |
||
| 1194 | $courseId = intval($courseId); |
||
| 1195 | $session_id = intval($session_id); |
||
| 1196 | |||
| 1197 | $select = '*'; |
||
| 1198 | if ($get_count) { |
||
| 1199 | $select = 'count(*) as count'; |
||
| 1200 | } |
||
| 1201 | $sql = "SELECT $select FROM $table_track_exercises |
||
| 1202 | WHERE status = '' AND |
||
| 1203 | c_id = '$courseId' AND |
||
| 1204 | session_id = $session_id AND |
||
| 1205 | orig_lp_id = 0 AND |
||
| 1206 | orig_lp_item_id = 0 |
||
| 1207 | ORDER BY exe_id"; |
||
| 1208 | $res = Database::query($sql); |
||
| 1209 | View Code Duplication | if ($get_count) { |
|
| 1210 | $row = Database::fetch_array($res, 'ASSOC'); |
||
| 1211 | return $row['count']; |
||
| 1212 | } else { |
||
| 1213 | $list = array(); |
||
| 1214 | while ($row = Database::fetch_array($res, 'ASSOC')) { |
||
| 1215 | $list[$row['exe_id']] = $row; |
||
| 1216 | } |
||
| 1217 | return $list; |
||
| 1218 | } |
||
| 1219 | } |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * Gets all exercise results (NO Exercises in LPs) from a given exercise id, course, session |
||
| 1223 | * @param int exercise id |
||
| 1224 | * @param int $courseId |
||
| 1225 | * @param int session id |
||
| 1226 | * @return array with the results |
||
| 1227 | * |
||
| 1228 | */ |
||
| 1229 | View Code Duplication | public static function get_all_exercise_results_by_user($user_id, $courseId, $session_id = 0) |
|
| 1261 | |||
| 1262 | /** |
||
| 1263 | * Gets exercise results (NO Exercises in LPs) from a given exercise id, course, session |
||
| 1264 | * @param int $exe_id exercise id |
||
| 1265 | * @param string $status |
||
| 1266 | * @return array with the results |
||
| 1267 | * |
||
| 1268 | */ |
||
| 1269 | public static function get_exercise_results_by_attempt($exe_id, $status = null) |
||
| 1307 | |||
| 1308 | /** |
||
| 1309 | * Gets exercise results (NO Exercises in LPs) from a given user, exercise id, course, session, lp_id, lp_item_id |
||
| 1310 | * @param int user id |
||
| 1311 | * @param int exercise id |
||
| 1312 | * @param string course code |
||
| 1313 | * @param int session id |
||
| 1314 | * @param int lp id |
||
| 1315 | * @param int lp item id |
||
| 1316 | * @param string order asc or desc |
||
| 1317 | * @return array with the results |
||
| 1318 | * |
||
| 1319 | */ |
||
| 1320 | public static function getExerciseResultsByUser( |
||
| 1321 | $user_id, |
||
| 1322 | $exercise_id, |
||
| 1323 | $courseId, |
||
| 1324 | $session_id = 0, |
||
| 1325 | $lp_id = 0, |
||
| 1326 | $lp_item_id = 0, |
||
| 1327 | $order = null |
||
| 1328 | ) { |
||
| 1329 | $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 1330 | $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); |
||
| 1331 | $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING); |
||
| 1332 | $courseId = intval($courseId); |
||
| 1333 | $exercise_id = intval($exercise_id); |
||
| 1334 | $session_id = intval($session_id); |
||
| 1335 | $user_id = intval($user_id); |
||
| 1336 | $lp_id = intval($lp_id); |
||
| 1337 | $lp_item_id = intval($lp_item_id); |
||
| 1338 | |||
| 1339 | if (!in_array(strtolower($order), array('asc', 'desc'))) { |
||
| 1340 | $order = 'asc'; |
||
| 1341 | } |
||
| 1342 | |||
| 1343 | $sql = "SELECT * FROM $table_track_exercises |
||
| 1344 | WHERE |
||
| 1345 | status = '' AND |
||
| 1346 | exe_user_id = $user_id AND |
||
| 1347 | c_id = $courseId AND |
||
| 1348 | exe_exo_id = $exercise_id AND |
||
| 1349 | session_id = $session_id AND |
||
| 1350 | orig_lp_id = $lp_id AND |
||
| 1351 | orig_lp_item_id = $lp_item_id |
||
| 1352 | ORDER by exe_id $order "; |
||
| 1353 | |||
| 1354 | $res = Database::query($sql); |
||
| 1355 | $list = array(); |
||
| 1356 | while ($row = Database::fetch_array($res, 'ASSOC')) { |
||
| 1357 | // Checking if this attempt was revised by a teacher |
||
| 1358 | $sql = 'SELECT exe_id FROM '.$table_track_attempt_recording.' |
||
| 1359 | WHERE author != "" AND exe_id = '.$row['exe_id'].' |
||
| 1360 | LIMIT 1'; |
||
| 1361 | $res_revised = Database::query($sql); |
||
| 1362 | $row['attempt_revised'] = 0; |
||
| 1363 | if (Database::num_rows($res_revised) > 0) { |
||
| 1364 | $row['attempt_revised'] = 1; |
||
| 1365 | } |
||
| 1366 | $list[$row['exe_id']] = $row; |
||
| 1367 | $sql = "SELECT * FROM $table_track_attempt |
||
| 1368 | WHERE exe_id = {$row['exe_id']}"; |
||
| 1369 | $res_question = Database::query($sql); |
||
| 1370 | View Code Duplication | while ($row_q = Database::fetch_array($res_question, 'ASSOC')) { |
|
| 1371 | $list[$row['exe_id']]['question_list'][$row_q['question_id']][] = $row_q; |
||
| 1372 | } |
||
| 1373 | } |
||
| 1374 | return $list; |
||
| 1375 | } |
||
| 1376 | |||
| 1377 | /** |
||
| 1378 | * Count exercise attempts (NO Exercises in LPs ) from a given exercise id, course, session |
||
| 1379 | * @param int $user_id |
||
| 1380 | * @param int exercise id |
||
| 1381 | * @param int $courseId |
||
| 1382 | * @param int session id |
||
| 1383 | * @return array with the results |
||
| 1384 | * |
||
| 1385 | */ |
||
| 1386 | public static function count_exercise_attempts_by_user($user_id, $exercise_id, $courseId, $session_id = 0) |
||
| 1412 | |||
| 1413 | /** |
||
| 1414 | * Gets all exercise BEST results attempts (NO Exercises in LPs) from a given exercise id, course, session per user |
||
| 1415 | * @param int $exercise_id |
||
| 1416 | * @param int $courseId |
||
| 1417 | * @param int $session_id |
||
| 1418 | * @return array with the results |
||
| 1419 | * @todo rename this function |
||
| 1420 | */ |
||
| 1421 | public static function get_best_exercise_results_by_user($exercise_id, $courseId, $session_id = 0) |
||
| 1469 | |||
| 1470 | /** |
||
| 1471 | * @param int $user_id |
||
| 1472 | * @param int $exercise_id |
||
| 1473 | * @param int $courseId |
||
| 1474 | * @param int $session_id |
||
| 1475 | * @return array |
||
| 1476 | */ |
||
| 1477 | public static function get_best_attempt_exercise_results_per_user($user_id, $exercise_id, $courseId, $session_id = 0) |
||
| 1516 | |||
| 1517 | /** |
||
| 1518 | * @param int $exercise_id |
||
| 1519 | * @param int $courseId |
||
| 1520 | * @param int $session_id |
||
| 1521 | * @return mixed |
||
| 1522 | */ |
||
| 1523 | public static function count_exercise_result_not_validated($exercise_id, $courseId, $session_id = 0) |
||
| 1524 | { |
||
| 1525 | $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 1526 | $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING); |
||
| 1527 | $courseId = intval($courseId); |
||
| 1528 | $session_id = intval($session_id); |
||
| 1529 | $exercise_id = intval($exercise_id); |
||
| 1530 | |||
| 1531 | $sql = "SELECT count(e.exe_id) as count |
||
| 1532 | FROM $table_track_exercises e |
||
| 1533 | LEFT JOIN $table_track_attempt a |
||
| 1534 | ON e.exe_id = a.exe_id |
||
| 1535 | WHERE |
||
| 1536 | exe_exo_id = $exercise_id AND |
||
| 1537 | c_id = '$courseId' AND |
||
| 1538 | e.session_id = $session_id AND |
||
| 1539 | orig_lp_id = 0 AND |
||
| 1540 | marks IS NULL AND |
||
| 1541 | status = '' AND |
||
| 1542 | orig_lp_item_id = 0 |
||
| 1543 | ORDER BY e.exe_id"; |
||
| 1544 | $res = Database::query($sql); |
||
| 1545 | $row = Database::fetch_array($res, 'ASSOC'); |
||
| 1546 | |||
| 1547 | return $row['count']; |
||
| 1548 | } |
||
| 1549 | |||
| 1550 | /** |
||
| 1551 | * Gets all exercise BEST results attempts (NO Exercises in LPs) from a given exercise id, course, session per user |
||
| 1552 | * @param int exercise id |
||
| 1553 | * @param int course id |
||
| 1554 | * @param int session id |
||
| 1555 | * @return array with the results |
||
| 1556 | * |
||
| 1557 | */ |
||
| 1558 | public static function get_count_exercises_attempted_by_course($courseId, $session_id = 0) |
||
| 1559 | { |
||
| 1560 | $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 1561 | $courseId = intval($courseId); |
||
| 1562 | $session_id = intval($session_id); |
||
| 1563 | |||
| 1564 | $sql = "SELECT DISTINCT exe_exo_id, exe_user_id |
||
| 1565 | FROM $table_track_exercises |
||
| 1566 | WHERE |
||
| 1567 | status = '' AND |
||
| 1568 | c_id = '$courseId' AND |
||
| 1569 | session_id = $session_id AND |
||
| 1570 | orig_lp_id =0 AND |
||
| 1571 | orig_lp_item_id = 0 |
||
| 1572 | ORDER BY exe_id"; |
||
| 1573 | $res = Database::query($sql); |
||
| 1574 | $count = 0; |
||
| 1575 | if (Database::num_rows($res) > 0) { |
||
| 1576 | $count = Database::num_rows($res); |
||
| 1577 | } |
||
| 1578 | return $count; |
||
| 1579 | } |
||
| 1580 | |||
| 1581 | /** |
||
| 1582 | * Gets all exercise events from a Learning Path within a Course nd Session |
||
| 1583 | * @param int $exercise_id |
||
| 1584 | * @param int $courseId |
||
| 1585 | * @param int $session_id |
||
| 1586 | * @return array |
||
| 1587 | */ |
||
| 1588 | View Code Duplication | public static function get_all_exercise_event_from_lp($exercise_id, $courseId, $session_id = 0) |
|
| 1618 | |||
| 1619 | /** |
||
| 1620 | * @param int $lp_id |
||
| 1621 | * @param int $course_id |
||
| 1622 | * |
||
| 1623 | * @return array |
||
| 1624 | */ |
||
| 1625 | View Code Duplication | public static function get_all_exercises_from_lp($lp_id, $course_id) |
|
| 1645 | |||
| 1646 | /** |
||
| 1647 | * This function gets the comments of an exercise |
||
| 1648 | * |
||
| 1649 | * @param int $exe_id |
||
| 1650 | * @param int $question_id |
||
| 1651 | * |
||
| 1652 | * @return string the comment |
||
| 1653 | */ |
||
| 1654 | View Code Duplication | public static function get_comments($exe_id, $question_id) |
|
| 1655 | { |
||
| 1656 | $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); |
||
| 1657 | $sql = "SELECT teacher_comment |
||
| 1658 | FROM $table_track_attempt |
||
| 1659 | WHERE |
||
| 1660 | exe_id='".Database::escape_string($exe_id)."' AND |
||
| 1661 | question_id = '".Database::escape_string($question_id)."' |
||
| 1662 | ORDER by question_id"; |
||
| 1663 | $sqlres = Database::query($sql); |
||
| 1664 | $comm = Database::result($sqlres, 0, 'teacher_comment'); |
||
| 1665 | |||
| 1666 | return $comm; |
||
| 1667 | } |
||
| 1668 | |||
| 1669 | /** |
||
| 1670 | * @param int $exe_id |
||
| 1671 | * |
||
| 1672 | * @return array |
||
| 1673 | */ |
||
| 1674 | View Code Duplication | public static function getAllExerciseEventByExeId($exe_id) |
|
| 1691 | |||
| 1692 | /** |
||
| 1693 | * |
||
| 1694 | * @param int $exe_id |
||
| 1695 | * @param int $user_id |
||
| 1696 | * @param int $courseId |
||
| 1697 | * @param int $session_id |
||
| 1698 | * @param int $question_id |
||
| 1699 | */ |
||
| 1700 | View Code Duplication | public static function delete_attempt($exe_id, $user_id, $courseId, $session_id, $question_id) |
|
| 1729 | |||
| 1730 | /** |
||
| 1731 | * @param $exe_id |
||
| 1732 | * @param $user_id |
||
| 1733 | * @param int $courseId |
||
| 1734 | * @param $question_id |
||
| 1735 | * @param int $sessionId |
||
| 1736 | */ |
||
| 1737 | View Code Duplication | public static function delete_attempt_hotspot($exe_id, $user_id, $courseId, $question_id, $sessionId = null) |
|
| 1766 | |||
| 1767 | /** |
||
| 1768 | * Registers in track_e_course_access when user logs in for the first time to a course |
||
| 1769 | * @param int $courseId ID of the course |
||
| 1770 | * @param int $user_id ID of the user |
||
| 1771 | * @param int $session_id ID of the session (if any) |
||
| 1772 | */ |
||
| 1773 | public static function event_course_login($courseId, $user_id, $session_id) |
||
| 1774 | { |
||
| 1775 | $course_tracking_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 1776 | $loginDate = $logoutDate = api_get_utc_datetime(); |
||
| 1777 | |||
| 1778 | //$counter represents the number of time this record has been refreshed |
||
| 1779 | $counter = 1; |
||
| 1780 | |||
| 1781 | $courseId = intval($courseId); |
||
| 1782 | $user_id = intval($user_id); |
||
| 1783 | $session_id = intval($session_id); |
||
| 1784 | $ip = api_get_real_ip(); |
||
| 1800 | |||
| 1801 | /** |
||
| 1802 | * Register a "fake" time spent on the platform, for example to match the |
||
| 1803 | * estimated time he took to author an assignment/work, see configuration |
||
| 1804 | * setting considered_working_time. |
||
| 1805 | * This assumes there is already some connection of the student to the |
||
| 1806 | * course, otherwise he wouldn't be able to upload an assignment. |
||
| 1807 | * This works by creating a new record, copy of the current one, then |
||
| 1808 | * updating the current one to be just the considered_working_time and |
||
| 1809 | * end at the same second as the user connected to the course. |
||
| 1810 | * @param int $courseId The course in which to add the time |
||
| 1811 | * @param int $userId The user for whom to add the time |
||
| 1812 | * @param int $sessionId The session in which to add the time (if any) |
||
| 1813 | * @param string $virtualTime The amount of time to be added, in a hh:mm:ss format. If int, we consider it is expressed in hours. |
||
| 1814 | * @param string $ip IP address to go on record for this time record |
||
| 1815 | * |
||
| 1816 | * @return True on successful insertion, false otherwise |
||
| 1817 | */ |
||
| 1818 | public static function eventAddVirtualCourseTime( |
||
| 1896 | /** |
||
| 1897 | * Removes a "fake" time spent on the platform, for example to match the |
||
| 1898 | * estimated time he took to author an assignment/work, see configuration |
||
| 1899 | * setting considered_working_time. |
||
| 1900 | * This method should be called when something that generated a fake |
||
| 1901 | * time record is removed. Given the database link is weak (no real |
||
| 1902 | * relationship kept between the deleted item and this record), this |
||
| 1903 | * method just looks for the latest record that has the same time as the |
||
| 1904 | * item's fake time, is in the past and in this course+session. If such a |
||
| 1905 | * record cannot be found, it doesn't do anything. |
||
| 1906 | * The IP address is not considered a useful filter here. |
||
| 1907 | * @param int $courseId The course in which to add the time |
||
| 1908 | * @param int $userId The user for whom to add the time |
||
| 1909 | * @param int $sessionId The session in which to add the time (if any) |
||
| 1910 | * @param string $virtualTime The amount of time to be added, in a hh:mm:ss format. If int, we consider it is expressed in hours. |
||
| 1911 | * @return True on successful removal, false otherwise |
||
| 1912 | */ |
||
| 1913 | public static function eventRemoveVirtualCourseTime($courseId, $userId, $sessionId = 0, $virtualTime = '') |
||
| 1961 | |||
| 1962 | /** |
||
| 1963 | * For the sake of genericity, this function is a switch. |
||
| 1964 | * It's called by EventsDispatcher and fires the good function |
||
| 1965 | * with the good require_once. |
||
| 1966 | * |
||
| 1967 | * @param string $event_name |
||
| 1968 | * @param array $params |
||
| 1969 | */ |
||
| 1970 | public static function event_send_mail($event_name, $params) |
||
| 1974 | |||
| 1975 | /** |
||
| 1976 | * Internal function checking if the mail was already sent from that user to that user |
||
| 1977 | * @param string $event_name |
||
| 1978 | * @param int $user_from |
||
| 1979 | * @param int $user_to |
||
| 1980 | * @return boolean |
||
| 1981 | */ |
||
| 1982 | public static function check_if_mail_already_sent($event_name, $user_from, $user_to = null) |
||
| 1995 | |||
| 1996 | /** |
||
| 1997 | * |
||
| 1998 | * Filter EventEmailTemplate Filters see the main/inc/conf/events.conf.dist.php |
||
| 1999 | * |
||
| 2000 | */ |
||
| 2001 | |||
| 2002 | /** |
||
| 2003 | * Basic template event message filter (to be used by other filters as default) |
||
| 2004 | * @param array $values (passing by reference) |
||
| 2005 | * @return boolean True if everything is OK, false otherwise |
||
| 2006 | */ |
||
| 2007 | public function event_send_mail_filter_func(&$values) |
||
| 2011 | |||
| 2012 | /** |
||
| 2013 | * user_registration - send_mail filter |
||
| 2014 | * @param array $values (passing by reference) |
||
| 2015 | * @return boolean True if everything is OK, false otherwise |
||
| 2016 | */ |
||
| 2017 | public function user_registration_event_send_mail_filter_func(&$values) |
||
| 2023 | |||
| 2024 | /** |
||
| 2025 | * portal_homepage_edited - send_mail filter |
||
| 2026 | * @param array $values (passing by reference) |
||
| 2027 | * @return boolean True if everything is OK, false otherwise |
||
| 2028 | */ |
||
| 2029 | public function portal_homepage_edited_event_send_mail_filter_func(&$values) |
||
| 2035 | } |
||
| 2036 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.