Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 59.32 KB | None | 0 0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16.  
  17. /**
  18.  * Progress Bar block common configuration and helper functions
  19.  *
  20.  * Instructions for adding new modules so they can be monitored
  21.  * ====================================================================================================================
  22.  * Activities that can be monitored (all resources are treated together) are defined in the
  23.  * block_progress_monitorable_modules() function.
  24.  *
  25.  * Modules can be added with:
  26.  *  - defaultTime (deadline from module if applicable),
  27.  *  - actions (array if action-query pairs) and
  28.  *  - defaultAction (selected by default in config page and needed for backwards compatibility)
  29.  *
  30.  * The module name needs to be the same as the table name for module in the database.
  31.  *
  32.  * Queries need to produce at least one result for completeness to go green, ie there is a record
  33.  * in the DB that indicates the user's completion.
  34.  *
  35.  * Queries may include the following placeholders that are substituted when the query is run. Note
  36.  * that each placeholder can only be used once in each query.
  37.  *  :eventid (the id of the activity in the DB table that relates to it, eg., an assignment id)
  38.  *  :cmid (the course module id that identifies the instance of the module within the course),
  39.  *  :userid (the current user's id) and
  40.  *  :courseid (the current course id)
  41.  *
  42.  * When you add a new module, you need to add a translation for it in the lang file.
  43.  * If you add new action names, you need to add a translation for these in the lang file.
  44.  *
  45.  * Note: Activity completion is automatically available when enabled (sitewide setting) and set for
  46.  * an activity.
  47.  *
  48.  * Passing relies on a passing grade being set for an activity in the Gradebook.
  49.  *
  50.  * If you have added a new module to this array and think other's may benefit from the query you
  51.  * have created, please share it by sending it to michaeld@moodle.com
  52.  * ====================================================================================================================
  53.  *
  54.  * @package    contrib
  55.  * @subpackage block_progress
  56.  * @copyright  2010 Michael de Raadt
  57.  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  58.  */
  59.  
  60. /**
  61.  * Provides information about monitorable modules
  62.  *
  63.  * @return array
  64.  */
  65. require_once(dirname(__FILE__) . '../../../lib/gradelib.php');
  66.  
  67. function block_progress_monitorable_modules() {
  68.     global $DB;
  69.  
  70.     return array(
  71.         'assign' => array(
  72.             'defaultTime' => 'duedate',
  73.             'actions' => array(
  74.                 'submitted'    => "SELECT id
  75.                                     FROM {assign_submission}
  76.                                    WHERE assignment = :eventid
  77.                                      AND userid = :userid
  78.                                      AND status = 'submitted'",
  79.                 'marked'       => "SELECT g.rawgrade
  80.                                     FROM {grade_grades} g, {grade_items} i
  81.                                    WHERE i.itemmodule = 'assign'
  82.                                      AND i.iteminstance = :eventid
  83.                                      AND i.id = g.itemid
  84.                                      AND g.userid = :userid
  85.                                      AND (g.finalgrade IS NOT NULL OR g.excluded <> 0)",
  86.                 'passed'       => "SELECT g.finalgrade, i.gradepass
  87.                                     FROM {grade_grades} g, {grade_items} i
  88.                                    WHERE i.itemmodule = 'assign'
  89.                                      AND i.iteminstance = :eventid
  90.                                      AND i.id = g.itemid
  91.                                      AND g.userid = :userid
  92.                                      AND (g.finalgrade IS NOT NULL OR g.excluded <> 0)",
  93.             ),
  94.             'defaultAction' => 'submitted'
  95.         ),
  96.         'assignment' => array(
  97.             'defaultTime' => 'timedue',
  98.             'actions' => array(
  99.                 'submitted'    => "SELECT id
  100.                                     FROM {assignment_submissions}
  101.                                    WHERE assignment = :eventid
  102.                                      AND userid = :userid
  103.                                      AND (
  104.                                          numfiles >= 1
  105.                                          OR {$DB->sql_compare_text('data2')} <> ''
  106.                                      )",
  107.                 'marked'       => "SELECT g.rawgrade
  108.                                     FROM {grade_grades} g, {grade_items} i
  109.                                    WHERE i.itemmodule = 'assignment'
  110.                                      AND i.iteminstance = :eventid
  111.                                      AND i.id = g.itemid
  112.                                      AND g.userid = :userid
  113.                                      AND (g.finalgrade IS NOT NULL OR g.excluded <> 0)",
  114.                 'passed'       => "SELECT g.finalgrade, i.gradepass
  115.                                     FROM {grade_grades} g, {grade_items} i
  116.                                    WHERE i.itemmodule = 'assignment'
  117.                                      AND i.iteminstance = :eventid
  118.                                      AND i.id = g.itemid
  119.                                      AND g.userid = :userid
  120.                                      AND (g.finalgrade IS NOT NULL OR g.excluded <> 0)",
  121.             ),
  122.             'defaultAction' => 'submitted'
  123.         ),
  124.         'bigbluebuttonbn' => array(
  125.             'defaultTime' => 'timedue',
  126.             'actions' => array(
  127.                 'viewed' => array (
  128.                     'logstore_legacy'     => "SELECT id
  129.                                                FROM {log}
  130.                                               WHERE course = :courseid
  131.                                                 AND module = 'bigbluebuttonbn'
  132.                                                 AND action = 'view'
  133.                                                 AND cmid = :cmid
  134.                                                 AND userid = :userid",
  135.                     'sql_internal_reader' => "SELECT id
  136.                                                FROM {log}
  137.                                               WHERE courseid = :courseid
  138.                                                 AND component = 'mod_bigbluebuttonbn'
  139.                                                 AND action = 'viewed'
  140.                                                 AND objectid = :eventid
  141.                                                 AND userid = :userid",
  142.                 ),
  143.             ),
  144.             'defaultAction' => 'viewed'
  145.         ),
  146.         'recordingsbn' => array(
  147.             'actions' => array(
  148.                 'viewed' => array (
  149.                     'logstore_legacy'     => "SELECT id
  150.                                                FROM {log}
  151.                                               WHERE course = :courseid
  152.                                                 AND module = 'recordingsbn'
  153.                                                 AND action = 'view'
  154.                                                 AND cmid = :cmid
  155.                                                 AND userid = :userid",
  156.                     'sql_internal_reader' => "SELECT id
  157.                                                FROM {log}
  158.                                               WHERE courseid = :courseid
  159.                                                 AND component = 'mod_recordingsbn'
  160.                                                 AND action = 'viewed'
  161.                                                 AND objectid = :eventid
  162.                                                 AND userid = :userid",
  163.                 ),
  164.             ),
  165.             'defaultAction' => 'viewed'
  166.         ),
  167.         'book' => array(
  168.             'actions' => array(
  169.                 'viewed' => array (
  170.                     'logstore_legacy'     => "SELECT id
  171.                                                FROM {log}
  172.                                               WHERE course = :courseid
  173.                                                 AND module = 'book'
  174.                                                 AND action = 'view'
  175.                                                 AND cmid = :cmid
  176.                                                 AND userid = :userid",
  177.                     'sql_internal_reader' => "SELECT id
  178.                                                FROM {log}
  179.                                               WHERE courseid = :courseid
  180.                                                 AND component = 'mod_book'
  181.                                                 AND action = 'viewed'
  182.                                                 AND objectid = :eventid
  183.                                                 AND userid = :userid",
  184.                 ),
  185.             ),
  186.             'defaultAction' => 'viewed'
  187.         ),
  188.         'certificate' => array(
  189.             'actions' => array(
  190.                 'awarded'      => "SELECT id
  191.                                     FROM {certificate_issues}
  192.                                    WHERE certificateid = :eventid
  193.                                      AND userid = :userid"
  194.             ),
  195.             'defaultAction' => 'awarded'
  196.         ),
  197.         'chat' => array(
  198.             'actions' => array(
  199.                 'posted_to'    => "SELECT id
  200.                                     FROM {chat_messages}
  201.                                    WHERE chatid = :eventid
  202.                                      AND userid = :userid"
  203.             ),
  204.             'defaultAction' => 'posted_to'
  205.         ),
  206.         'choice' => array(
  207.             'defaultTime' => 'timeclose',
  208.             'actions' => array(
  209.                 'answered'     => "SELECT id
  210.                                     FROM {choice_answers}
  211.                                    WHERE choiceid = :eventid
  212.                                      AND userid = :userid"
  213.             ),
  214.             'defaultAction' => 'answered'
  215.         ),
  216.         'data' => array(
  217.             'defaultTime' => 'timeviewto',
  218.             'actions' => array(
  219.                 'viewed' => array (
  220.                     'logstore_legacy'     => "SELECT id
  221.                                                FROM {log}
  222.                                               WHERE course = :courseid
  223.                                                 AND module = 'data'
  224.                                                 AND action = 'view'
  225.                                                 AND cmid = :cmid
  226.                                                 AND userid = :userid",
  227.                     'sql_internal_reader' => "SELECT id
  228.                                                FROM {log}
  229.                                               WHERE courseid = :courseid
  230.                                                 AND component = 'mod_data'
  231.                                                 AND action = 'viewed'
  232.                                                 AND objectid = :eventid
  233.                                                 AND userid = :userid",
  234.                 ),
  235.             ),
  236.             'defaultAction' => 'viewed'
  237.         ),
  238.         'feedback' => array(
  239.             'defaultTime' => 'timeclose',
  240.             'actions' => array(
  241.                 'responded_to' => "SELECT id
  242.                                     FROM {feedback_completed}
  243.                                    WHERE feedback = :eventid
  244.                                      AND userid = :userid"
  245.             ),
  246.             'defaultAction' => 'responded_to'
  247.         ),
  248.         'resource' => array(  // AKA file.
  249.             'actions' => array(
  250.                 'viewed' => array (
  251.                     'logstore_legacy'     => "SELECT id
  252.                                                FROM {log}
  253.                                               WHERE course = :courseid
  254.                                                 AND module = 'resource'
  255.                                                 AND action = 'view'
  256.                                                 AND cmid = :cmid
  257.                                                 AND userid = :userid",
  258.                     'sql_internal_reader' => "SELECT id
  259.                                                FROM {log}
  260.                                               WHERE courseid = :courseid
  261.                                                 AND component = 'mod_resource'
  262.                                                 AND action = 'viewed'
  263.                                                 AND objectid = :eventid
  264.                                                 AND userid = :userid",
  265.                 ),
  266.             ),
  267.             'defaultAction' => 'viewed'
  268.         ),
  269.         'flashcardtrainer' => array(
  270.             'actions' => array(
  271.                 'viewed' => array (
  272.                     'logstore_legacy'     => "SELECT id
  273.                                                FROM {log}
  274.                                               WHERE course = :courseid
  275.                                                 AND module = 'flashcardtrainer'
  276.                                                 AND action = 'view'
  277.                                                 AND cmid = :cmid
  278.                                                 AND userid = :userid",
  279.                     'sql_internal_reader' => "SELECT id
  280.                                                FROM {log}
  281.                                               WHERE courseid = :courseid
  282.                                                 AND component = 'mod_flashcardtrainer'
  283.                                                 AND action = 'viewed'
  284.                                                 AND objectid = :eventid
  285.                                                 AND userid = :userid",
  286.                 ),
  287.             ),
  288.             'defaultAction' => 'viewed'
  289.         ),
  290.         'folder' => array(
  291.             'actions' => array(
  292.                 'viewed' => array (
  293.                     'logstore_legacy'     => "SELECT id
  294.                                                FROM {log}
  295.                                               WHERE course = :courseid
  296.                                                 AND module = 'folder'
  297.                                                 AND action = 'view'
  298.                                                 AND cmid = :cmid
  299.                                                 AND userid = :userid",
  300.                     'sql_internal_reader' => "SELECT id
  301.                                                FROM {log}
  302.                                               WHERE courseid = :courseid
  303.                                                 AND component = 'mod_folder'
  304.                                                 AND action = 'viewed'
  305.                                                 AND objectid = :eventid
  306.                                                 AND userid = :userid",
  307.                 ),
  308.             ),
  309.             'defaultAction' => 'viewed'
  310.         ),
  311.         'forum' => array(
  312.             'defaultTime' => 'assesstimefinish',
  313.             'actions' => array(
  314.                 'posted_to'    => "SELECT id
  315.                                     FROM {forum_posts}
  316.                                    WHERE userid = :userid AND discussion IN (
  317.                                          SELECT id
  318.                                            FROM {forum_discussions}
  319.                                           WHERE forum = :eventid
  320.                                    )"
  321.             ),
  322.             'defaultAction' => 'posted_to'
  323.         ),
  324.         'glossary' => array(
  325.             'actions' => array(
  326.                 'viewed' => array (
  327.                     'logstore_legacy'     => "SELECT id
  328.                                                FROM {log}
  329.                                               WHERE course = :courseid
  330.                                                 AND module = 'glossary'
  331.                                                 AND action = 'view'
  332.                                                 AND cmid = :cmid
  333.                                                 AND userid = :userid",
  334.                     'sql_internal_reader' => "SELECT id
  335.                                                FROM {log}
  336.                                               WHERE courseid = :courseid
  337.                                                 AND component = 'mod_glossary'
  338.                                                 AND action = 'viewed'
  339.                                                 AND objectid = :eventid
  340.                                                 AND userid = :userid",
  341.                 ),
  342.             ),
  343.             'defaultAction' => 'viewed'
  344.         ),
  345.         'hotpot' => array(
  346.             'defaultTime' => 'timeclose',
  347.             'actions' => array(
  348.                 'attempted'    => "SELECT id
  349.                                     FROM {hotpot_attempts}
  350.                                    WHERE hotpotid = :eventid
  351.                                      AND userid = :userid",
  352.                 'finished'     => "SELECT id
  353.                                     FROM {hotpot_attempts}
  354.                                    WHERE hotpotid = :eventid
  355.                                      AND userid = :userid
  356.                                      AND timefinish <> 0",
  357.             ),
  358.             'defaultAction' => 'finished'
  359.         ),
  360.         'hsuforum' => array(
  361.             'defaultTime' => 'assesstimefinish',
  362.             'actions' => array(
  363.                 'posted_to'    => "SELECT id
  364.                                     FROM {hsuforum_posts}
  365.                                    WHERE userid = :userid AND discussion IN (
  366.                                          SELECT id
  367.                                            FROM {hsuforum_discussions}
  368.                                           WHERE forum = :eventid
  369.                                    )"
  370.             ),
  371.             'defaultAction' => 'posted_to'
  372.         ),
  373.         'imscp' => array(
  374.             'actions' => array(
  375.                 'viewed' => array (
  376.                     'logstore_legacy'     => "SELECT id
  377.                                                FROM {log}
  378.                                               WHERE course = :courseid
  379.                                                 AND module = 'imscp'
  380.                                                 AND action = 'view'
  381.                                                 AND cmid = :cmid
  382.                                                 AND userid = :userid",
  383.                     'sql_internal_reader' => "SELECT id
  384.                                                FROM {log}
  385.                                               WHERE courseid = :courseid
  386.                                                 AND component = 'mod_imscp'
  387.                                                 AND action = 'viewed'
  388.                                                 AND objectid = :eventid
  389.                                                 AND userid = :userid",
  390.                 ),
  391.             ),
  392.             'defaultAction' => 'viewed'
  393.         ),
  394.         'journal' => array(
  395.             'actions' => array(
  396.                 'posted_to'    => "SELECT id
  397.                                     FROM {journal_entries}
  398.                                    WHERE journal = :eventid
  399.                                      AND userid = :userid"
  400.             ),
  401.             'defaultAction' => 'posted_to'
  402.         ),
  403.         'lesson' => array(
  404.             'defaultTime' => 'deadline',
  405.             'actions' => array(
  406.                 'attempted'    => "SELECT id
  407.                                     FROM {lesson_attempts}
  408.                                    WHERE lessonid = :eventid
  409.                                      AND userid = :userid
  410.                                UNION ALL
  411.                                   SELECT id
  412.                                     FROM {lesson_branch}
  413.                                    WHERE lessonid = :eventid1
  414.                                      AND userid = :userid1",
  415.                 'graded'       => "SELECT g.rawgrade
  416.                                     FROM {grade_grades} g, {grade_items} i
  417.                                    WHERE i.itemmodule = 'lesson'
  418.                                      AND i.iteminstance = :eventid
  419.                                      AND i.id = g.itemid
  420.                                      AND g.userid = :userid
  421.                                      AND (g.finalgrade IS NOT NULL OR g.excluded <> 0)",
  422.             ),
  423.             'defaultAction' => 'attempted'
  424.         ),
  425.         'page' => array(
  426.             'actions' => array(
  427.                 'viewed' => array (
  428.                     'logstore_legacy'     => "SELECT id
  429.                                                FROM {log}
  430.                                               WHERE course = :courseid
  431.                                                 AND module = 'page'
  432.                                                 AND action = 'view'
  433.                                                 AND cmid = :cmid
  434.                                                 AND userid = :userid",
  435.                     'sql_internal_reader' => "SELECT id
  436.                                                FROM {log}
  437.                                               WHERE courseid = :courseid
  438.                                                 AND component = 'mod_page'
  439.                                                 AND action = 'viewed'
  440.                                                 AND objectid = :eventid
  441.                                                 AND userid = :userid",
  442.                 ),
  443.             ),
  444.             'defaultAction' => 'viewed'
  445.         ),
  446.         'questionnaire' => array(
  447.             'defaultTime' => 'closedate',
  448.             'actions' => array(
  449.                 'attempted'    => "SELECT id
  450.                                     FROM {questionnaire_attempts}
  451.                                    WHERE qid = :eventid
  452.                                      AND userid = :userid",
  453.                 'finished'     => "SELECT id
  454.                                     FROM {questionnaire_response}
  455.                                    WHERE complete = 'y'
  456.                                      AND username = :userid
  457.                                      AND survey_id = :eventid",
  458.             ),
  459.             'defaultAction' => 'finished'
  460.         ),
  461.         'quiz' => array(
  462.             'defaultTime' => 'timeclose',
  463.             'actions' => array(
  464.                 'attempted'    => "SELECT id
  465.                                     FROM {quiz_attempts}
  466.                                    WHERE quiz = :eventid
  467.                                      AND userid = :userid",
  468.                 'finished'     => "SELECT id
  469.                                     FROM {quiz_attempts}
  470.                                    WHERE quiz = :eventid
  471.                                      AND userid = :userid
  472.                                      AND timefinish <> 0",
  473.                 'graded'       => "SELECT g.rawgrade
  474.                                     FROM {grade_grades} g, {grade_items} i
  475.                                    WHERE i.itemmodule = 'quiz'
  476.                                      AND i.iteminstance = :eventid
  477.                                      AND i.id = g.itemid
  478.                                      AND g.userid = :userid
  479.                                      AND (g.finalgrade IS NOT NULL OR g.excluded <> 0)",
  480.                 'passed'       => "SELECT g.finalgrade, i.gradepass
  481.                                     FROM {grade_grades} g, {grade_items} i
  482.                                    WHERE i.itemmodule = 'quiz'
  483.                                      AND i.iteminstance = :eventid
  484.                                      AND i.id = g.itemid
  485.                                      AND g.userid = :userid
  486.                                      AND (g.finalgrade IS NOT NULL OR g.excluded <> 0)",
  487.             ),
  488.             'defaultAction' => 'finished'
  489.         ),
  490.  
  491.         'groupmanagement' => array(
  492.             'actions' => array(
  493.                 'viewed' => array (
  494.                     'logstore_legacy'     => "SELECT id
  495.                                                FROM {log}
  496.                                               WHERE course = :courseid
  497.                                                 AND module = 'groupmanagement'
  498.                                                 AND action = 'view'
  499.                                                 AND cmid = :cmid
  500.                                                 AND userid = :userid",
  501.                     'sql_internal_reader' => "SELECT id
  502.                                                FROM {log}
  503.                                               WHERE courseid = :courseid
  504.                                                 AND component = 'mod_groupmanagement'
  505.                                                 AND action = 'viewed'
  506.                                                 AND objectid = :eventid
  507.                                                 AND userid = :userid",
  508.                 ),
  509.                 'selected'    => "SELECT id
  510.                                     FROM {groups_members}
  511.                                    WHERE userid = :userid AND groupid IN (
  512.                                          SELECT id
  513.                                            FROM {groups}
  514.                                           WHERE courseid = :courseid AND id IN (
  515.                                           SELECT groupid
  516.                                            FROM {groupmanagement_options}
  517.                                            WHERE groupmanagementid = :eventid
  518.                                           )
  519.                                    )"
  520.             ),
  521.             'defaultAction' => 'viewed'
  522.         ),
  523.  
  524.         'moodecforum' => array(
  525.             'actions' => array(
  526.                     'posted_to'    => "SELECT postid
  527.                                     FROM qa_posts
  528.                                    WHERE categoryid = :eventid
  529.                                      AND userid = :userid"
  530.  
  531.             ),
  532.             'defaultAction' => 'posted_to'
  533.         ),
  534.  
  535.  
  536.         'scorm' => array(
  537.             'actions' => array(
  538.                 'attempted'    => "SELECT id
  539.                                     FROM {scorm_scoes_track}
  540.                                    WHERE scormid = :eventid
  541.                                      AND userid = :userid",
  542.                 'completed'    => "SELECT id
  543.                                     FROM {scorm_scoes_track}
  544.                                    WHERE scormid = :eventid
  545.                                      AND userid = :userid
  546.                                      AND element = 'cmi.core.lesson_status'
  547.                                      AND {$DB->sql_compare_text('value')} = 'completed'",
  548.                 'passedscorm'  => "SELECT id
  549.                                     FROM {scorm_scoes_track}
  550.                                    WHERE scormid = :eventid
  551.                                      AND userid = :userid
  552.                                      AND element = 'cmi.core.lesson_status'
  553.                                      AND {$DB->sql_compare_text('value')} = 'passed'"
  554.             ),
  555.             'defaultAction' => 'attempted'
  556.         ),
  557.         'turnitintool' => array(
  558.             'defaultTime' => 'defaultdtdue',
  559.             'actions' => array(
  560.                 'submitted'    => "SELECT id
  561.                                     FROM {turnitintool_submissions}
  562.                                    WHERE turnitintoolid = :eventid
  563.                                      AND userid = :userid
  564.                                      AND submission_score IS NOT NULL"
  565.             ),
  566.             'defaultAction' => 'submitted'
  567.         ),
  568.         'url' => array(
  569.             'actions' => array(
  570.                 'viewed' => array (
  571.                     'logstore_legacy'     => "SELECT id
  572.                                                FROM {log}
  573.                                               WHERE course = :courseid
  574.                                                 AND module = 'url'
  575.                                                 AND action = 'view'
  576.                                                 AND cmid = :cmid
  577.                                                 AND userid = :userid",
  578.                     'sql_internal_reader' => "SELECT id
  579.                                                FROM {log}
  580.                                               WHERE courseid = :courseid
  581.                                                 AND component = 'mod_url'
  582.                                                 AND action = 'viewed'
  583.                                                 AND objectid = :eventid
  584.                                                 AND userid = :userid",
  585.                 ),
  586.             ),
  587.             'defaultAction' => 'viewed'
  588.         ),
  589.         'wiki' => array(
  590.             'actions' => array(
  591.                 'viewed' => array (
  592.                     'logstore_legacy'     => "SELECT id
  593.                                                FROM {log}
  594.                                               WHERE course = :courseid
  595.                                                 AND module = 'wiki'
  596.                                                 AND action = 'view'
  597.                                                 AND cmid = :cmid
  598.                                                 AND userid = :userid",
  599.                     'sql_internal_reader' => "SELECT id
  600.                                                FROM {log}
  601.                                               WHERE courseid = :courseid
  602.                                                 AND component = 'mod_wiki'
  603.                                                 AND action = 'viewed'
  604.                                                 AND objectid = :eventid
  605.                                                 AND userid = :userid",
  606.                 ),
  607.             ),
  608.             'defaultAction' => 'viewed'
  609.         ),
  610.         'workshop' => array(
  611.             'defaultTime' => 'assessmentend',
  612.             'actions' => array(
  613.                 'submitted'    => "SELECT id
  614.                                     FROM {workshop_submissions}
  615.                                    WHERE workshopid = :eventid
  616.                                      AND authorid = :userid",
  617.                 'assessed'     => "SELECT s.id
  618.                                     FROM {workshop_assessments} a, {workshop_submissions} s
  619.                                    WHERE s.workshopid = :eventid
  620.                                      AND s.id = a.submissionid
  621.                                      AND a.reviewerid = :userid
  622.                                      AND a.grade IS NOT NULL",
  623.                 'graded'       => "SELECT g.rawgrade
  624.                                     FROM {grade_grades} g, {grade_items} i
  625.                                    WHERE i.itemmodule = 'workshop'
  626.                                      AND i.iteminstance = :eventid
  627.                                      AND i.id = g.itemid
  628.                                      AND g.userid = :userid
  629.                                      AND (g.finalgrade IS NOT NULL OR g.excluded <> 0)",
  630.             ),
  631.             'defaultAction' => 'submitted'
  632.         ),
  633.     );
  634. }
  635.  
  636. /**
  637.  * Checks if a variable has a value and returns a default value if it doesn't
  638.  *
  639.  * @param mixed $var The variable to check
  640.  * @param mixed $def Default value if $var is not set
  641.  * @return string
  642.  */
  643. function progress_default_value(&$var, $def = null) {
  644.     return isset($var)?$var:$def;
  645. }
  646.  
  647. /**
  648.  * Filters the modules list to those installed in Moodle instance and used in current course
  649.  *
  650.  * @return array
  651.  */
  652. function block_progress_modules_in_use($course) {
  653.     global $DB;
  654.  
  655.     $dbmanager = $DB->get_manager(); // Used to check if tables exist.
  656.     $modules = block_progress_monitorable_modules();
  657.     $modulesinuse = array();
  658.  
  659.     foreach ($modules as $module => $details) {
  660.         if (
  661.             $dbmanager->table_exists($module) &&
  662.             $DB->record_exists($module, array('course' => $course))
  663.         ) {
  664.             $modulesinuse[$module] = $details;
  665.         }
  666.     }
  667.     return $modulesinuse;
  668. }
  669.  
  670. /**
  671.  * Gets event information about modules monitored by an instance of a Progress Bar block
  672.  *
  673.  * @param stdClass $config  The block instance configuration values
  674.  * @param array    $modules The modules used in the course
  675.  * @param stdClass $course  The current course
  676.  * @param int      $userid  The user's ID
  677.  * @return mixed   returns array of visible events monitored,
  678.  *                 empty array if none of the events are visible,
  679.  *                 null if all events are configured to "no" monitoring and
  680.  *                 0 if events are available but no config is set
  681.  */
  682. function block_progress_event_information($config, $modules, $course, $userid = 0) {
  683.     global $DB, $USER;
  684.     $events = array();
  685.     $numevents = 0;
  686.     $numeventsconfigured = 0;
  687.  
  688.     if ($userid === 0) {
  689.         $userid = $USER->id;
  690.     }
  691.  
  692.     // Get section information for the course module layout.
  693.     $sections = block_progress_course_sections($course);
  694.  
  695.     // Check each known module (described in lib.php).
  696.     foreach ($modules as $module => $details) {
  697.         $fields = 'id, name';
  698.         if (array_key_exists('defaultTime', $details)) {
  699.             $fields .= ', '.$details['defaultTime'].' as due';
  700.         }
  701.  
  702.         // Check if this type of module is used in the course, gather instance info.
  703.         $records = $DB->get_records($module, array('course' => $course), '', $fields);
  704.         foreach ($records as $record) {
  705.  
  706.             // Is the module being monitored?
  707.             if (isset($config->{'monitor_'.$module.$record->id})) {
  708.                 $numeventsconfigured++;
  709.             }
  710.             if (progress_default_value($config->{'monitor_'.$module.$record->id}, 0) == 1) {
  711.                 $numevents++;
  712.                 // Check the time the module is due.
  713.                 if (
  714.                     isset($details['defaultTime']) &&
  715.                     $record->due != 0 &&
  716.                     progress_default_value($config->{'locked_'.$module.$record->id}, 0)
  717.                 ) {
  718.                     $expected = progress_default_value($record->due);
  719.                 } else {
  720.                     $expected = $config->{'date_time_'.$module.$record->id};
  721.                 }
  722.  
  723.                 // Gather together module information.
  724.                 $coursemodule = block_progress_get_coursemodule($module, $record->id, $course);
  725.                 $events[] = array(
  726.                     'expected' => $expected,
  727.                     'type'     => $module,
  728.                     'id'       => $record->id,
  729.                     'name'     => format_string($record->name),
  730.                     'cm'       => $coursemodule,
  731.                     'section'  => $sections[$coursemodule->section]->section,
  732.                     'position' => array_search($coursemodule->id, $sections[$coursemodule->section]->sequence),
  733.                 );
  734.             }
  735.         }
  736.     }
  737.  
  738.     if ($numeventsconfigured == 0) {
  739.         return 0;
  740.     }
  741.     if ($numevents == 0) {
  742.         return null;
  743.     }
  744.  
  745.     // Sort by first value in each element, which is time due.
  746.     if (isset($config->orderby) && $config->orderby == 'orderbycourse') {
  747.         usort($events, 'block_progress_compare_events');
  748.     } else {
  749.         usort($events, 'block_progress_compare_times');
  750.     }
  751.     return $events;
  752. }
  753.  
  754. /**
  755.  * Used to compare two activities/resources based on order on course page
  756.  *
  757.  * @param array $a array of event information
  758.  * @param array $b array of event information
  759.  * @return <0, 0 or >0 depending on order of activities/resources on course page
  760.  */
  761. function block_progress_compare_events($a, $b) {
  762.     if ($a['section'] != $b['section']) {
  763.         return $a['section'] - $b['section'];
  764.     } else {
  765.         return $a['position'] - $b['position'];
  766.     }
  767. }
  768.  
  769. /**
  770.  * Used to compare two activities/resources based their expected completion times
  771.  *
  772.  * @param array $a array of event information
  773.  * @param array $b array of event information
  774.  * @return <0, 0 or >0 depending on time then order of activities/resources
  775.  */
  776. function block_progress_compare_times($a, $b) {
  777.     if ($a['expected'] != $b['expected']) {
  778.         return $a['expected'] - $b['expected'];
  779.     } else {
  780.         return block_progress_compare_events($a, $b);
  781.     }
  782. }
  783.  
  784. /**
  785.  * Checked if a user has attempted/viewed/etc. an activity/resource
  786.  *
  787.  * @param array    $modules  The modules used in the course
  788.  * @param stdClass $config   The blocks configuration settings
  789.  * @param array    $events   The possible events that can occur for modules
  790.  * @param int      $userid   The user's id
  791.  * @param int      $instance The instance of the block
  792.  * @return array   an describing the user's attempts based on module+instance identifiers
  793.  */
  794. function block_progress_attempts($modules, $config, $events, $userid, $course) {
  795.     global $DB;
  796.     $attempts = array();
  797.     $modernlogging = false;
  798.     $cachingused = false;
  799.  
  800.     // Get readers for 2.7 onwards.
  801.     if (function_exists('get_log_manager')) {
  802.         $modernlogging = true;
  803.         $logmanager = get_log_manager();
  804.         $readers = $logmanager->get_readers();
  805.         $numreaders = count($readers);
  806.     }
  807.  
  808.     // Get cache store if caching is working 2.4 onwards.
  809.     if (class_exists('cache')) {
  810.         $cachingused = true;
  811.         $cachedlogs = cache::make('block_progress', 'cachedlogs');
  812.         $cachedlogviews = $cachedlogs->get($userid);
  813.         if (empty($cachedlogviews)) {
  814.             $cachedlogviews = array();
  815.         }
  816.         $cachedlogsupdated = false;
  817.     }
  818.  
  819.     foreach ($events as $event) {
  820.         $module = $modules[$event['type']];
  821.         $uniqueid = $event['type'].$event['id'];
  822.         $parameters = array('courseid' => $course, 'courseid1' => $course,
  823.                             'userid' => $userid, 'userid1' => $userid,
  824.                             'eventid' => $event['id'], 'eventid1' => $event['id'],
  825.                             'cmid' => $event['cm']->id, 'cmid1' => $event['cm']->id,
  826.                       );
  827.  
  828.         // Check for passing grades as unattempted, passed or failed.
  829.         if (isset($config->{'action_'.$uniqueid}) && $config->{'action_'.$uniqueid} == 'passed') {
  830.             $query = $module['actions'][$config->{'action_'.$uniqueid}];
  831.             $graderesult = $DB->get_record_sql($query, $parameters);
  832.             if ($graderesult === false || $graderesult->finalgrade === null) {
  833.                 $attempts[$uniqueid] = false;
  834.             } else {
  835.                 $attempts[$uniqueid] = $graderesult->finalgrade >= $graderesult->gradepass ? true : 'failed';
  836.             }
  837.         }
  838.  
  839.         // Checked view actions in the log table/store/cache.
  840.         else if (isset($config->{'action_'.$uniqueid}) && $config->{'action_'.$uniqueid} == 'viewed') {
  841.             $attempts[$uniqueid] = false;
  842.  
  843.             // Check if the value is cached.
  844.             if ($cachingused && array_key_exists($uniqueid, $cachedlogviews) && $cachedlogviews[$uniqueid]) {
  845.                 $attempts[$uniqueid] = true;
  846.             }
  847.  
  848.             // Check in the logs.
  849.             else {
  850.                 if ($modernlogging) {
  851.                     foreach ($readers as $logstore => $reader) {
  852.                         if ($reader instanceof logstore_legacy\log\store) {
  853.                             $query = $module['actions']['viewed']['logstore_legacy'];
  854.                         }
  855.                         else if ($reader instanceof \core\log\sql_internal_reader) {
  856.                             $logtable = '{'.$reader->get_internal_log_table_name().'}';
  857.                             $query = preg_replace('/\{log\}/', $logtable, $module['actions']['viewed']['sql_internal_reader']);
  858.                         }
  859.                         $attempts[$uniqueid] = $DB->record_exists_sql($query, $parameters) ? true : false;
  860.                         if ($attempts[$uniqueid]) {
  861.                             $cachedlogviews[$uniqueid] = true;
  862.                             $cachedlogsupdated = true;
  863.                             break;
  864.                         }
  865.                     }
  866.                 } else {
  867.                     $query = $module['actions']['viewed']['logstore_legacy'];
  868.                     $attempts[$uniqueid] = $DB->record_exists_sql($query, $parameters) ? true : false;
  869.                     if ($cachingused && $attempts[$uniqueid]) {
  870.                         $cachedlogviews[$uniqueid] = true;
  871.                         $cachedlogsupdated = true;
  872.                     }
  873.                 }
  874.             }
  875.         } else {
  876.  
  877.             // If activity completion is used, check completions table.
  878.             if (isset($config->{'action_'.$uniqueid}) && $config->{'action_'.$uniqueid} == 'activity_completion') {
  879.                 $query = 'SELECT id
  880.                            FROM {course_modules_completion}
  881.                           WHERE userid = :userid
  882.                             AND coursemoduleid = :cmid
  883.                             AND completionstate >= 1';
  884.             }
  885.  
  886.             // Determine the set action and develop a query.
  887.             else {
  888.                 $action = isset($config->{'action_'.$uniqueid})?
  889.                           $config->{'action_'.$uniqueid}:
  890.                           $module['defaultAction'];
  891.                 $query = $module['actions'][$action];
  892.             }
  893.  
  894.              // Check if the user has attempted the module.
  895.             $attempts[$uniqueid] = $DB->record_exists_sql($query, $parameters) ? true : false;
  896.         }
  897.     }
  898.  
  899.     // Update log cache if new values were added.
  900.     if ($cachingused && $cachedlogsupdated) {
  901.         $cachedlogs->set($userid, $cachedlogviews);
  902.     }
  903.  
  904.     return $attempts;
  905. }
  906.  
  907. /**
  908.  * Draws a progress bar
  909.  *
  910.  * @param array    $modules  The modules used in the course
  911.  * @param stdClass $config   The blocks configuration settings
  912.  * @param array    $events   The possible events that can occur for modules
  913.  * @param int      $userid   The user's id
  914.  * @param int      instance  The block instance (in case more than one is being displayed)
  915.  * @param array    $attempts The user's attempts on course activities
  916.  * @param bool     $simple   Controls whether instructions are shown below a progress bar
  917.  * @return string  Progress Bar HTML content
  918.  */
  919. function block_progress_bar($modules, $config, $events, $userid, $instance, $attempts, $course, $simple = false) {
  920.     global $OUTPUT, $CFG, $DB;
  921.     $now = time();
  922.     $numevents = count($events);
  923.     $dateformat = get_string('strftimerecentfull', 'langconfig');
  924.     $tableoptions = array('class' => 'progressBarProgressTable',
  925.                           'cellpadding' => '0',
  926.                           'cellspacing' => '0');
  927.  
  928.     // Place now arrow.
  929.     if ((!isset($config->orderby) || $config->orderby == 'orderbytime') && $config->displayNow == 1 && !$simple) {
  930.  
  931.         $content = HTML_WRITER::start_tag('table', $tableoptions);
  932.  
  933.         // Find where to put now arrow.
  934.         $nowpos = 0;
  935.         while ($nowpos < $numevents && $now > $events[$nowpos]['expected']) {
  936.             $nowpos++;
  937.         }
  938.         $content .= HTML_WRITER::start_tag('tr');
  939.         $nowstring = get_string('now_indicator', 'block_progress');
  940.         if ($nowpos < $numevents / 2) {
  941.             for ($i = 0; $i < $nowpos; $i++) {
  942.                 $content .= HTML_WRITER::tag('td', '&nbsp;', array('class' => 'progressBarHeader'));
  943.             }
  944.             $celloptions = array('colspan' => $numevents - $nowpos,
  945.                                  'class' => 'progressBarHeader',
  946.                                  'style' => 'text-align:left;');
  947.             $content .= HTML_WRITER::start_tag('td', $celloptions);
  948.             $content .= $OUTPUT->pix_icon('left', $nowstring, 'block_progress');
  949.             $content .= $nowstring;
  950.             $content .= HTML_WRITER::end_tag('td');
  951.         } else {
  952.             $celloptions = array('colspan' => $nowpos,
  953.                                  'class' => 'progressBarHeader',
  954.                                  'style' => 'text-align:right;');
  955.             $content .= HTML_WRITER::start_tag('td', $celloptions);
  956.             $content .= $nowstring;
  957.             $content .= $OUTPUT->pix_icon('right', $nowstring, 'block_progress');
  958.             $content .= HTML_WRITER::end_tag('td');
  959.             for ($i = $nowpos; $i < $numevents; $i++) {
  960.                 $content .= HTML_WRITER::tag('td', '&nbsp;', array('class' => 'progressBarHeader'));
  961.             }
  962.         }
  963.         $content .= HTML_WRITER::end_tag('tr');
  964.     }
  965.     else {
  966.         $tableoptions['class'] = 'progressBarProgressTable noNow';
  967.         $content = HTML_WRITER::start_tag('table', $tableoptions);
  968.     }
  969.  
  970.     // Start progress bar.
  971.     $width = 100 / $numevents;
  972.     $content .= HTML_WRITER::start_tag('tr');
  973.     $counter = 1;
  974.  
  975.     foreach ($events as $event) {
  976.         $attempted = $attempts[$event['type'].$event['id']];
  977.         $action = isset($config->{'action_'.$event['type'].$event['id']})?
  978.                   $config->{'action_'.$event['type'].$event['id']}:
  979.                   $modules[$event['type']]['defaultAction'];
  980.  
  981.         // A cell in the progress bar.
  982.         $celloptions = array(
  983.             'class' => 'progressBarCell',
  984.             'id' => '',
  985.             'width' => $width.'%',
  986.             'onmouseover' => 'M.block_progress.showInfo('.$instance.','.$userid.','.$event['cm']->id.');',
  987.              'style' => 'background-color:');
  988.         if ($attempted === true) {
  989.             $celloptions['style'] .= get_config('block_progress', 'attempted_colour').';';
  990.             $cellcontent = $OUTPUT->pix_icon(
  991.                                isset($config->progressBarIcons) && $config->progressBarIcons == 1 ?
  992.                                'tick' : 'blank', '', 'block_progress');
  993.         }
  994.  
  995.         else if (((!isset($config->orderby) || $config->orderby == 'orderbytime') && $event['expected'] < $now) ||
  996.                  ($attempted === 'failed')) {
  997.             $celloptions['style'] .= get_config('block_progress', 'notattempted_colour').';';
  998.             $cellcontent = $OUTPUT->pix_icon(
  999.                                isset($config->progressBarIcons) && $config->progressBarIcons == 1 ?
  1000.                                'cross':'blank', '', 'block_progress');
  1001.         }
  1002.         else {
  1003.             $celloptions['style'] .= get_config('block_progress', 'futurenotattempted_colour').';';
  1004.             $cellcontent = $OUTPUT->pix_icon('blank', '', 'block_progress');
  1005.         }
  1006.         if (!empty($event['cm']->available)) {
  1007.             $celloptions['onclick'] = 'document.location=\''.
  1008.                 $CFG->wwwroot.'/mod/'.$event['type'].'/view.php?id='.$event['cm']->id.'\';';
  1009.         }
  1010.         if ($counter == 1) {
  1011.             $celloptions['id'] .= 'first';
  1012.         }
  1013.         if ($counter == $numevents) {
  1014.             $celloptions['id'] .= 'last';
  1015.         }
  1016.         $counter++;
  1017.         $content .= HTML_WRITER::tag('td', $cellcontent, $celloptions);
  1018.     }
  1019.     $content .= HTML_WRITER::end_tag('tr');
  1020.     $content .= HTML_WRITER::end_tag('table');
  1021.  
  1022.     // Add the info box below the table.
  1023.     $divoptions = array('class' => 'progressEventInfo',
  1024.                         'id' => 'progressBarInfo'.$instance.'-'.$userid.'-info');
  1025.     $content .= HTML_WRITER::start_tag('div', $divoptions);
  1026.     if (!$simple) {
  1027.         if (isset($config->showpercentage) && $config->showpercentage == 1) {
  1028.             $progress = block_progress_percentage($events, $attempts);
  1029.             $content .= get_string('progress', 'block_progress').': ';
  1030.             $content .= $progress.'%'.HTML_WRITER::empty_tag('br');
  1031.         }
  1032.         $content .= get_string('mouse_over_prompt', 'block_progress');
  1033.     }
  1034.     $content .= HTML_WRITER::end_tag('div');
  1035.  
  1036.     // Add hidden divs for activity information.
  1037.     $displaydate = (!isset($config->orderby) || $config->orderby == 'orderbytime') &&
  1038.                    (!isset($config->displayNow) || $config->displayNow == 1);
  1039.     foreach ($events as $event) {
  1040.         $attempted = $attempts[$event['type'].$event['id']];
  1041.         $action = isset($config->{'action_'.$event['type'].$event['id']})?
  1042.                   $config->{'action_'.$event['type'].$event['id']}:
  1043.                   $modules[$event['type']]['defaultAction'];
  1044.         $divoptions = array('class' => 'progressEventInfo',
  1045.                             'id' => 'progressBarInfo'.$instance.'-'.$userid.'-'.$event['cm']->id,
  1046.                             'style' => 'display: none;');
  1047.         $content .= HTML_WRITER::start_tag('div', $divoptions);
  1048.         $link = '/mod/'.$event['type'].'/view.php?id='.$event['cm']->id;
  1049.         $text = $OUTPUT->pix_icon('icon', '', $event['type'], array('class' => 'moduleIcon')).s($event['name']);
  1050.         if (!empty($event['cm']->available)) {
  1051.             $content .= $OUTPUT->action_link($link, $text);
  1052.         } else {
  1053.             $content .= $text;
  1054.         }
  1055.         $content .= HTML_WRITER::empty_tag('br');
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.  
  1062.         $content .= get_string($action, 'block_progress').'&nbsp;';
  1063.         $icon = ($attempted && $attempted !== 'failed' ? 'tick' : 'cross');
  1064.         $content .= $OUTPUT->pix_icon($icon, '', 'block_progress');
  1065.  
  1066.         // Add Grade
  1067.  
  1068.         if($config->showgrade == 1){
  1069.             $content.= get_grade($event, $course, $DB, $userid);
  1070.         }
  1071.  
  1072.         $content .= HTML_WRITER::empty_tag('br');
  1073.         if ($displaydate) {
  1074.             $content .= HTML_WRITER::start_tag('div', array('class' => 'expectedBy'));
  1075.             $content .= get_string('time_expected', 'block_progress').': ';
  1076.             $content .= userdate($event['expected'], $dateformat, $CFG->timezone);
  1077.             $content .= HTML_WRITER::end_tag('div');
  1078.         }
  1079.         $content .= HTML_WRITER::end_tag('div');
  1080.     }
  1081.  
  1082.     return $content;
  1083. }
  1084.  
  1085. /**
  1086.  * Calculates an overall percentage of progress
  1087.  *
  1088.  * @param array $events   The possible events that can occur for modules
  1089.  * @param array $attempts The user's attempts on course activities
  1090.  * @return int  Progress value as a percentage
  1091.  */
  1092. function get_grade($event, $course, $DB, $userid) {
  1093.  
  1094.     $content = "" ;
  1095.     $commentWorkshop = array() ;
  1096.     $commentWorkshop[0] =  get_string('workshopSubmission', 'block_progress') ;
  1097.     $commentWorkshop[1] = get_string('workshopAssessment', 'block_progress') ;
  1098.  
  1099.     // Parameters
  1100.         $param = array($course, $event['type'], $event['id']);
  1101.         // Check if activity can have a grade
  1102.         if($DB->record_exists_sql("SELECT id FROM {grade_items} WHERE courseid= ? AND itemmodule= ? AND iteminstance= ?", $param)) {
  1103.             $content .= HTML_WRITER::start_tag('div');
  1104.  
  1105.  
  1106.  
  1107.                 // Get itemid
  1108.                 $query = "SELECT id FROM {grade_items} WHERE courseid= ? AND itemmodule= ? AND iteminstance= ?";
  1109.                 $itemid = $DB->get_fieldset_sql($query, $param);
  1110.  
  1111.                 // Get user marks
  1112.                 $querymarks = "SELECT id, itemid, rawgrademax ,finalgrade, excluded FROM {grade_grades} WHERE itemid = ? AND userid= ? ";
  1113.  
  1114.             // Check if there's is a scale or only a grade
  1115.                 $query = "SELECT scaleid FROM {grade_items} WHERE courseid= ? AND itemmodule= ? AND iteminstance= ?" ;
  1116.                 $scaleid = $DB->get_fieldset_sql($query, $param) ;
  1117.  
  1118.                 if(isset($scaleid)){ // if Scale get the scaleElement
  1119.                     $scaleElement = get_scale_element($scaleid[0], $DB) ;
  1120.                 }
  1121.  
  1122.  
  1123.                 for( $j=0 ; $j<count($itemid) ; $j++) {
  1124.  
  1125.  
  1126.                     $param = array($itemid[$j], $userid); // Changing the parameters
  1127.  
  1128.                     $grades = $DB->get_records_sql($querymarks, $param);
  1129.  
  1130.                     foreach ($grades as $grade) {
  1131.                         $i = 0 ;
  1132.                         $mark[$i] = $grades[$grade->id];
  1133.                         $mark[$i]->itemid = $grade->itemid;
  1134.                         $mark[$i]->rawgrademax = $grade->rawgrademax;
  1135.                         $mark[$i]->finalgrade = $grade->finalgrade;
  1136.                         $mark[$i]->excluded = $grade->excluded;
  1137.                     }
  1138.  
  1139.                     if (!isset($mark[0]->finalgrade) || !isset($mark[0]->rawgrademax)) {
  1140.                         // No content added
  1141.                     } else {
  1142.  
  1143.                        if(isset($scaleid[0])) {
  1144.                                 $content .= get_string('apppreciation', 'block_progress') . $scaleElement[$mark[0]->finalgrade-1] ; // do -1 to match the explode table of the scale and the index of the mark
  1145.                         }else {
  1146.  
  1147.                             if ($event['type'] == "workshop") { // Add comment to the grade for workshop
  1148.                                 $content .= $commentWorkshop[$j] . number_format($mark[0]->finalgrade, 2, '.', '') . "/" . number_format($mark[0]->rawgrademax, 2, '.', '');
  1149.                             } else {
  1150.                                 $content .= number_format($mark[0]->finalgrade, 2, '.', '') . "/" . number_format($mark[0]->rawgrademax, 2, '.', '');
  1151.                             }
  1152.                         }
  1153.                         if($mark[0]->excluded > 0){ // Check if the grade is excluded from the average and notify the student
  1154.                             $content .= get_string('excludedGrade', 'block_progress');
  1155.                         }
  1156.                         $content .= HTML_WRITER::empty_tag('br');
  1157.                     }
  1158.                 }
  1159.  
  1160.             $content .= HTML_WRITER::end_tag('div');
  1161.  
  1162.         }
  1163.  
  1164.     return $content ;
  1165.  
  1166. }
  1167. function get_scale_element($scaleid, $DB) {
  1168.  
  1169.     // Get scale element
  1170.     $param = array($scaleid);
  1171.  
  1172.     $query = "SELECT scale FROM {scale} WHERE id= ?"; // Select Scale
  1173.     $scale = $DB->get_fieldset_sql($query, $param);
  1174.  
  1175.     $scale = $scale[0]; // Get first Element (The string)
  1176.  
  1177.     $scaleElement = explode(",",$scale) ; // explode by ,
  1178.  
  1179.     return $scaleElement ;
  1180.  
  1181.  
  1182. }
  1183.  
  1184. function block_progress_percentage($events, $attempts) {
  1185.     $attemptcount = 0;
  1186.  
  1187.     foreach ($events as $event) {
  1188.         if ($attempts[$event['type'].$event['id']] == 1) {
  1189.             $attemptcount++;
  1190.         }
  1191.     }
  1192.  
  1193.     $progressvalue = $attemptcount == 0 ? 0 : $attemptcount / count($events);
  1194.  
  1195.     return (int)round($progressvalue * 100);
  1196. }
  1197.  
  1198. /**
  1199.  * Gathers the course section and activity/resource information for ordering
  1200.  *
  1201.  * @return array section information
  1202.  */
  1203. function block_progress_course_sections($course) {
  1204.     global $DB;
  1205.  
  1206.     $sections = $DB->get_records('course_sections', array('course' => $course), 'section', 'id,section,name,sequence');
  1207.     foreach ($sections as $key => $section) {
  1208.         if ($section->sequence != '') {
  1209.             $sections[$key]->sequence = explode(',', $section->sequence);
  1210.         }
  1211.         else {
  1212.             $sections[$key]->sequence = null;
  1213.         }
  1214.     }
  1215.  
  1216.     return $sections;
  1217. }
  1218.  
  1219. /**
  1220.  * Filters events that a user cannot see due to grouping constraints
  1221.  *
  1222.  * @param array  $events The possible events that can occur for modules
  1223.  * @param array  $userid The user's id
  1224.  * @param string $coursecontext the context value of the course
  1225.  * @param string $course the course for filtering visibility
  1226.  * @return array The array with restricted events removed
  1227.  */
  1228. function block_progress_filter_visibility($events, $userid, $coursecontext, $course = 0) {
  1229.     global $CFG, $USER;
  1230.     $filteredevents = array();
  1231.  
  1232.     // Check if the events are empty or none are selected.
  1233.     if ($events === 0) {
  1234.         return 0;
  1235.     }
  1236.     if ($events === null) {
  1237.         return null;
  1238.     }
  1239.  
  1240.     // Keep only events that are visible.
  1241.     foreach ($events as $key => $event) {
  1242.  
  1243.         // Determine the correct user info to check.
  1244.         if ($userid == $USER->id) {
  1245.             $coursemodule = $event['cm'];
  1246.         }
  1247.         else {
  1248.             $coursemodule = block_progress_get_coursemodule($event['type'], $event['id'], $course->id, $userid);
  1249.         }
  1250.  
  1251.         // Check visibility in course.
  1252.         if (!$coursemodule->visible && !has_capability('moodle/course:viewhiddenactivities', $coursecontext, $userid)) {
  1253.             continue;
  1254.         }
  1255.  
  1256.         // Check availability, allowing for visible, but not accessible items.
  1257.         if (!empty($CFG->enableavailability)) {
  1258.             if (
  1259.                 isset($coursemodule->available) && !$coursemodule->available && empty($coursemodule->availableinfo) &&
  1260.                 !has_capability('moodle/course:viewhiddenactivities', $coursecontext, $userid)
  1261.             ) {
  1262.                 continue;
  1263.             }
  1264.         }
  1265.         // Check visibility by grouping constraints (includes capability check).
  1266.         if (!empty($CFG->enablegroupmembersonly)) {
  1267.             if (isset($coursemodule->uservisible)) {
  1268.                 if ($coursemodule->uservisible != 1 && empty($coursemodule->availableinfo)) {
  1269.                     continue;
  1270.                 }
  1271.             }
  1272.             else if (!groups_course_module_visible($coursemodule, $userid)) {
  1273.                 continue;
  1274.             }
  1275.         }
  1276.  
  1277.         // Save the visible event.
  1278.         $filteredevents[] = $event;
  1279.     }
  1280.     return $filteredevents;
  1281. }
  1282.  
  1283. /**
  1284.  * Checks whether the current page is the My home page.
  1285.  *
  1286.  * @return bool True when on the My home page.
  1287.  */
  1288. function block_progress_on_my_page() {
  1289.     global $SCRIPT;
  1290.  
  1291.     return $SCRIPT === '/my/index.php';
  1292. }
  1293.  
  1294. /**
  1295.  * Gets the course context, allowing for old and new Moodle instances.
  1296.  *
  1297.  * @param int $courseid The course ID
  1298.  * @return stdClass The context object
  1299.  */
  1300. function block_progress_get_course_context($courseid) {
  1301.     if (class_exists('context_course')) {
  1302.         return context_course::instance($courseid);
  1303.     } else {
  1304.         return get_context_instance(CONTEXT_COURSE, $courseid);
  1305.     }
  1306. }
  1307.  
  1308. /**
  1309.  * Gets the block context, allowing for old and new Moodle instances.
  1310.  *
  1311.  * @param int $block The block ID
  1312.  * @return stdClass The context object
  1313.  */
  1314. function block_progress_get_block_context($blockid) {
  1315.     if (class_exists('context_block')) {
  1316.         return context_block::instance($blockid);
  1317.     } else {
  1318.         return get_context_instance(CONTEXT_BLOCK, $blockid);
  1319.     }
  1320. }
  1321.  
  1322. /**
  1323.  * Gets the course module in a backwards compatible way.
  1324.  *
  1325.  * @param int $module   the type of module (eg, assign, quiz...)
  1326.  * @param int $recordid the instance ID (from its table)
  1327.  * @param int $courseid the course ID
  1328.  * @return stdClass The course module object
  1329.  */
  1330. function block_progress_get_coursemodule($module, $recordid, $courseid, $userid = 0) {
  1331.     global $CFG;
  1332.  
  1333.     if ($CFG->version >= 2012120300) {
  1334.         return get_fast_modinfo($courseid, $userid)->instances[$module][$recordid];
  1335.     }
  1336.     else {
  1337.         return get_coursemodule_from_instance($module, $recordid, $courseid);
  1338.     }
  1339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement