<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Contains classes, functions and constants used during the tracking
 * of activity completion for users.
 *
 * Completion top-level options (admin setting enablecompletion)
 *
 * @package core_completion
 * @category completion
 * @copyright 1999 onwards Martin Dougiamas   {@link http://moodle.com}
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

/**
 * Include the required completion libraries
 */
require_once $CFG->dirroot.'/completion/completion_aggregation.php';
require_once $CFG->dirroot.'/completion/criteria/completion_criteria.php';
require_once $CFG->dirroot.'/completion/completion_completion.php';
require_once $CFG->dirroot.'/completion/completion_criteria_completion.php';


/**
 * The completion system is enabled in this site/course
 */
define('COMPLETION_ENABLED', 1);
/**
 * The completion system is not enabled in this site/course
 */
define('COMPLETION_DISABLED', 0);

/**
 * Completion tracking is disabled for this activity
 * This is a completion tracking option per-activity  (course_modules/completion)
 */
define('COMPLETION_TRACKING_NONE', 0);

/**
 * Manual completion tracking (user ticks box) is enabled for this activity
 * This is a completion tracking option per-activity  (course_modules/completion)
 */
define('COMPLETION_TRACKING_MANUAL', 1);
/**
 * Automatic completion tracking (system ticks box) is enabled for this activity
 * This is a completion tracking option per-activity  (course_modules/completion)
 */
define('COMPLETION_TRACKING_AUTOMATIC', 2);

/**
 * The user has not completed this activity.
 * This is a completion state value (course_modules_completion/completionstate)
 */
define('COMPLETION_INCOMPLETE', 0);
/**
 * The user has completed this activity. It is not specified whether they have
 * passed or failed it.
 * This is a completion state value (course_modules_completion/completionstate)
 */
define('COMPLETION_COMPLETE', 1);
/**
 * The user has completed this activity with a grade above the pass mark.
 * This is a completion state value (course_modules_completion/completionstate)
 */
define('COMPLETION_COMPLETE_PASS', 2);
/**
 * The user has completed this activity but their grade is less than the pass mark
 * This is a completion state value (course_modules_completion/completionstate)
 */
define('COMPLETION_COMPLETE_FAIL', 3);
/** A record of prior learning has been added for a user in this activity */
// TOTARA: This isn't used anywhere in Totara, and hasn't since 2.2.0 at least, so we're going to
//         remove it after the release of Totara 10.
define('COMPLETION_COMPLETE_RPL', 4);

/**
 * The effect of this change to completion status is unknown.
 * A completion effect changes (used only in update_state)
 */
define('COMPLETION_UNKNOWN', -1);
/**
 * The user's grade has changed, so their new state might be
 * COMPLETION_COMPLETE_PASS or COMPLETION_COMPLETE_FAIL.
 * A completion effect changes (used only in update_state)
 */
define('COMPLETION_GRADECHANGE', -2);

/**
 * User must view this activity.
 * Whether view is required to create an activity (course_modules/completionview)
 */
define('COMPLETION_VIEW_REQUIRED', 1);
/**
 * User does not need to view this activity
 * Whether view is required to create an activity (course_modules/completionview)
 */
define('COMPLETION_VIEW_NOT_REQUIRED', 0);

/**
 * User has viewed this activity.
 * Completion viewed state (course_modules_completion/viewed)
 */
define('COMPLETION_VIEWED', 1);
/**
 * User has not viewed this activity.
 * Completion viewed state (course_modules_completion/viewed)
 */
define('COMPLETION_NOT_VIEWED', 0);

/**
 * Cache expiry time in seconds (10 minutes)
 * Completion cacheing
 */
define('COMPLETION_CACHE_EXPIRY', 10*60);

/**
 * Completion details should be ORed together and you should return false if
 * none apply.
 */
define('COMPLETION_OR', false);
/**
 * Completion details should be ANDed together and you should return true if
 * none apply
 */
define('COMPLETION_AND', true);

/**
 * Course completion criteria aggregation method.
 */
define('COMPLETION_AGGREGATION_ALL', 1);
/**
 * Course completion criteria aggregation method.
 */
define('COMPLETION_AGGREGATION_ANY', 2);


/**
 * Utility function for checking if the logged in user can view
 * another's completion data for a particular course
 *
 * @access  public
 * @param   int         $userid     Completion data's owner
 * @param   mixed       $course     Course object or Course ID (optional)
 * @return  boolean
 */
function completion_can_view_data($userid, $course = null) {
    global $USER;

    if (!isloggedin()) {
        return false;
    }

    if (!is_object($course)) {
        $cid = $course;
        $course = new stdClass();
        $course->id = $cid;
    }

    // Check if this is the site course
    if ($course->id == SITEID) {
        $course = null;
    }

    // Check if completion is enabled
    if ($course) {
        $cinfo = new completion_info($course);
        if (!$cinfo->is_enabled()) {
            return false;
        }
    } else {
        if (!completion_info::is_enabled_for_site()) {
            return false;
        }
    }

    // Is own user's data?
    if ($USER->id == $userid) {
        return true;
    }

    // Check capabilities
    if (!$personalcontext = context_user::instance($userid, IGNORE_MISSING)) {
        return false;
    }

    if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)) {
        return true;
    } elseif (has_capability('report/completion:view', $personalcontext)) {
        return true;
    }

    if (isset($course->id)) {
        $coursecontext = context_course::instance($course->id);
    } else {
        $coursecontext = context_system::instance();
    }

    if (has_capability('report/completion:view', $coursecontext)) {
        return true;
    }

    return false;
}


/**
 * Utility function for checking if the logged in user has permissions
 * to unlock completion data for a course
 *
 * @access  public
 * @param   int         $courseid   Course ID
 * @return  boolean
 */
function completion_can_unlock_data($courseid) {
    if (!isloggedin()) {
        return false;
    }

    // Check if this is the site course.
    if (!$courseid || $courseid == SITEID) {
        return false;
    }

    // Check capabilities.
    $coursecontext = context_course::instance($courseid);
    return has_capability('moodle/course:unlockcompletion', $coursecontext);
}


/**
 * Check if a module type has RPL enabled for it
 *
 * RPLs for modules are enabled/disabled via $CFG->enablemodulerpl, which can
 * be set on the Site Administration settings page "Grades -> General settings"
 *
 * @access  public
 * @param   mixed   $module     Can either be the (string) name of the module,
 *                              or the module type id (int).
 * @return  bool
 */
function completion_module_rpl_enabled($module) {
    global $CFG, $DB;

    // If supplied module id, this is easy
    if (is_integer($module)) {
        // Check if RPL is enabled for this module
        return in_array($module, explode(',', $CFG->enablemodulerpl));
    }

    // If supplied the module string name
    if (is_string($module)) {

        // Cache modules list
        static $modules;
        if ($modules == null) {
            $modules = $DB->get_records('modules', array(), '', 'name, id');
            if (!$modules) {
                $modules = array();
            }
        }

        // Check if module exists in DB
        if (!array_key_exists($module, $modules)) {
            return false;
        }

        // Check if RPL is enabled for this module
        return in_array($modules[$module]->id, explode(',', $CFG->enablemodulerpl));
    }

    // Incorrect input
    throw new coding_exception(get_string('error:incorrectdatatypesupplied', 'completion'));
}

/**
 * Returns the self completion form for an activity
 *
 * @param cm_info|stdClass|int $cmorid The course module object or the cmid.
 * @param stdClass|null $course The course object if we already have it.
 * @return string HTML of the self completion form
 */
function self_completion_form($cmorid, $course = null) {
    global $OUTPUT;

    if (!isloggedin() || isguestuser()) {
        // You must be logged in, and you cannot be the guest user.
        return '';
    }

    list($course, $cm) = get_course_and_cm_from_cmid($cmorid, '', $course);

    // Check the module supports manual completion.
    // We do this before we check if completion is enabled as this is cheap.
    if ($cm->completion != COMPLETION_TRACKING_MANUAL) {
        return '';
    }

    if (!$cm->uservisible) {
        // The user cannot yet see this module.
        // We do this before we check if completion is enabled as this is cheap.
        return '';
    }

    // Check completion is enabled.
    $completion = new \completion_info($course);
    if (!$completion->is_enabled()) {
        // Completion is not enabled. No point in going further.
        return '';
    }

    $cmdata = $completion->get_data($cm);

    $form = new \core_completion\form\activity_completion([
        'activity_id' => $cm->id,
        'completed' => $cmdata->completionstate
    ]);

    // Use notification template as notification class sanitises the form output
    $notificationdata = new stdClass();
    $notificationdata->message = $form->render();
    return $OUTPUT->render_from_template('core/notification_info', $notificationdata);
}


/**
 * Class represents completion information for a course.
 *
 * Does not contain any data, so you can safely construct it multiple times
 * without causing any problems.
 *
 * @package core
 * @category completion
 * @copyright 2008 Sam Marshall
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class completion_info {

    /* @var stdClass Course object passed during construction */
    private $course;

    /* @var int Course id */
    public $course_id;

    /* @var array Completion criteria {@link completion_info::get_criteria()}  */
    private $criteria;

    /**
     * Course progress aggregation information
     * @since Totara 10
     * @var \totara_core\progressinfo
     */
    private $progressinfo;

    /**
     * Return array of aggregation methods
     * @return array
     */
    public static function get_aggregation_methods() {
        return array(
            COMPLETION_AGGREGATION_ALL => get_string('all'),
            COMPLETION_AGGREGATION_ANY => get_string('any', 'completion'),
        );
    }

    /**
     * Return array containing detail on completion criteria types that may have multiple requirements
     *
     * @since Totara 10
     * @return array
     */
    public static function get_multi_activity_criteria() {
        return array(
            COMPLETION_CRITERIA_TYPE_ACTIVITY => 'moduleinstance',
            COMPLETION_CRITERIA_TYPE_COURSE => 'courseinstance',
            COMPLETION_CRITERIA_TYPE_ROLE => 'role'
        );
    }


    /**
     * Constructs with course details.
     *
     * When instantiating a new completion info object you must provide a course
     * object with at least id, and enablecompletion properties.
     *
     * @param stdClass $course Moodle course object.
     */
    public function __construct($course) {
        $this->course = $course;
        $this->course_id = $course->id;
    }

    /**
     * Determines whether completion is enabled across entire site.
     *
     * @return bool COMPLETION_ENABLED (true) if completion is enabled for the site,
     *     COMPLETION_DISABLED (false) if it's complete
     */
    public static function is_enabled_for_site() {
        global $CFG;
        return !(!isset($CFG->enablecompletion) || $CFG->enablecompletion == COMPLETION_DISABLED);
    }

    /**
     * Checks whether completion is enabled in a particular course and possibly
     * activity.
     *
     * @param stdClass|cm_info $cm Course-module object. If not specified, returns the course
     *   completion enable state.
     * @return mixed COMPLETION_ENABLED or COMPLETION_DISABLED (==0) in the case of
     *   site and course; COMPLETION_TRACKING_MANUAL, _AUTOMATIC or _NONE (==0)
     *   for a course-module.
     */
    public function is_enabled($cm = null) {
        global $CFG, $DB;

        // First check global completion
        if (!completion_info::is_enabled_for_site()) {
            return COMPLETION_DISABLED;
        }

        // Load data if we do not have enough
        if (!isset($this->course->enablecompletion)) {
            $this->course->enablecompletion = $DB->get_field('course', 'enablecompletion', array('id' => $this->course->id));
        }

        // Check course completion
        if ($this->course->enablecompletion == COMPLETION_DISABLED) {
            return COMPLETION_DISABLED;
        }

        // If there was no $cm and we got this far, then it's enabled
        if (!$cm) {
            return COMPLETION_ENABLED;
        }

        // Return course-module completion value
        return $cm->completion;
    }

    /**
     * Returns the 'Your progress' help icon, if completion tracking is enabled.
     *
     * @return string HTML code for help icon, or blank if not needed
     */
    public function display_help_icon() {
        global $PAGE, $OUTPUT;
        $result = '';
        if ($this->is_enabled() && !$PAGE->user_is_editing() && isloggedin() && !isguestuser()) {
            $result .= html_writer::tag('div', get_string('yourprogress','completion') .
                    $OUTPUT->help_icon('completionicons', 'completion'), array('id' => 'completionprogressid',
                    'class' => 'completionprogress'));
        }
        return $result;
    }

    /**
     * Get a course completion for a user
     *
     * @param int $user_id User id
     * @param int $criteriatype Specific criteria type to return
     * @return bool|completion_criteria_completion returns false on fail
     */
    public function get_completion($user_id, $criteriatype) {
        $completions = $this->get_completions($user_id, $criteriatype);

        if (empty($completions)) {
            return false;
        } elseif (count($completions) > 1) {
            print_error('multipleselfcompletioncriteria', 'completion');
        }

        return $completions[0];
    }

    /**
     * Get all course criteria's completion objects for a user
     *
     * @param int $user_id User id
     * @param int $criteriatype Specific criteria type to return (optional)
     * @return array
     */
    public function get_completions($user_id, $criteriatype = null) {
        $criterion = $this->get_criteria($criteriatype);

        $completions = array();

        foreach ($criterion as $criteria) {
            $params = array(
                'course'        => $this->course_id,
                'userid'        => $user_id,
                'criteriaid'    => $criteria->id
            );

            $completion = new completion_criteria_completion($params);
            $completion->attach_criteria($criteria);

            $completions[] = $completion;
        }

        return $completions;
    }

    /**
     * Get completion object for a user and a criteria
     *
     * @param int $user_id User id
     * @param completion_criteria $criteria Criteria object
     * @return completion_criteria_completion
     */
    public function get_user_completion($user_id, $criteria) {
        $params = array(
            'course'        => $this->course_id,
            'userid'        => $user_id,
            'criteriaid'    => $criteria->id,
        );

        $completion = new completion_criteria_completion($params);
        return $completion;
    }

    /**
     * Check if course has completion criteria set
     *
     * @return bool Returns true if there are criteria
     */
    public function has_criteria() {
        $criteria = $this->get_criteria();

        return (bool) count($criteria);
    }

    /**
     * Get course completion criteria
     *
     * @param int $criteriatype Specific criteria type to return (optional)
     */
    public function get_criteria($criteriatype = null) {

        // Fill cache if empty
        if (!is_array($this->criteria)) {
            global $DB;

            $params = array(
                'course'    => $this->course->id
            );

            // Load criteria from database.
            // Sort by criteriatype first, then to get reliable sort results.
            $records = (array)$DB->get_records('course_completion_criteria', $params, 'criteriatype ASC, role ASC');

            // Order records so activities are in the same order as they appear on the course view page.
            if ($records) {
                $activitiesorder = array_keys(get_fast_modinfo($this->course)->get_cms());
                usort($records, function ($a, $b) use ($activitiesorder) {
                    $aidx = ($a->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) ?
                        array_search($a->moduleinstance, $activitiesorder) : false;
                    $bidx = ($b->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) ?
                        array_search($b->moduleinstance, $activitiesorder) : false;
                    if ($aidx === false || $bidx === false || $aidx == $bidx) {
                        return 0;
                    }
                    return ($aidx < $bidx) ? -1 : 1;
                });
            }

            // Build array of criteria objects
            $this->criteria = array();
            foreach ($records as $record) {
                $this->criteria[$record->id] = completion_criteria::factory((array)$record);
            }
        }

        // If after all criteria
        if ($criteriatype === null) {
            return $this->criteria;
        }

        // If we are only after a specific criteria type
        $criteria = array();
        foreach ($this->criteria as $criterion) {

            if ($criterion->criteriatype != $criteriatype) {
                continue;
            }

            $criteria[$criterion->id] = $criterion;
        }

        return $criteria;
    }

    /**
     * Get aggregation method
     *
     * @param int $criteriatype If none supplied, get overall aggregation method (optional)
     * @return int One of COMPLETION_AGGREGATION_ALL or COMPLETION_AGGREGATION_ANY
     */
    public function get_aggregation_method($criteriatype = null) {
        $params = array(
            'course'        => $this->course_id,
            'criteriatype'  => $criteriatype
        );

        $aggregation = new completion_aggregation($params);

        if (!$aggregation->id) {
            $aggregation->method = COMPLETION_AGGREGATION_ALL;
        }

        return $aggregation->method;
    }

    /**
     * Clear old course completion criteria
     */
    public function clear_criteria() {
        global $DB;
        $DB->delete_records('course_completion_criteria', array('course' => $this->course_id));
        $DB->delete_records('course_completion_aggr_methd', array('course' => $this->course_id));

        $this->delete_course_completion_data_including_rpl();
    }

    /**
     * Has the supplied user completed this course
     *
     * @param int $user_id User's id
     * @return boolean
     */
    public function is_course_complete($user_id) {
        $params = array(
            'userid'    => $user_id,
            'course'  => $this->course_id
        );

        $ccompletion = new completion_completion($params);
        return $ccompletion->is_complete();
    }

    /**
     * Updates (if necessary) the completion state of activity $cm for the given
     * user.
     *
     * For manual completion, this function is called when completion is toggled
     * with $possibleresult set to the target state.
     *
     * For automatic completion, this function should be called every time a module
     * does something which might influence a user's completion state. For example,
     * if a forum provides options for marking itself 'completed' once a user makes
     * N posts, this function should be called every time a user makes a new post.
     * [After the post has been saved to the database]. When calling, you do not
     * need to pass in the new completion state. Instead this function carries out
     * completion calculation by checking grades and viewed state itself, and
     * calling the involved module via modulename_get_completion_state() to check
     * module-specific conditions.
     *
     * @global object
     * @global object
     * @uses COMPLETION_UNKNOWN
     * @uses COMPLETION_COMPLETE
     * @uses COMPLETION_INCOMPLETE
     * @uses COMPLETION_COMPLETE_PASS
     * @uses COMPLETION_COMPLETE_FAIL
     * @uses COMPLETION_TRACKING_MANUAL
     * @param stdClass|cm_info $cm Course-module
     * @param int $possibleresult Expected completion result. If the event that
     *   has just occurred (e.g. add post) can only result in making the activity
     *   complete when it wasn't before, use COMPLETION_COMPLETE. If the event that
     *   has just occurred (e.g. delete post) can only result in making the activity
     *   not complete when it was previously complete, use COMPLETION_INCOMPLETE.
     *   Otherwise use COMPLETION_UNKNOWN. Setting this value to something other than
     *   COMPLETION_UNKNOWN significantly improves performance because it will abandon
     *   processing early if the user's completion state already matches the expected
     *   result. For manual events, COMPLETION_COMPLETE or COMPLETION_INCOMPLETE
     *   must be used; these directly set the specified state.
     * @param int $userid User ID to be updated. Default 0 = current user
     * @param stdClass $current the current data record, if known (prevents loop when course_modules_completion record is missing)
     * @return void
     */
    public function update_state($cm, $possibleresult = COMPLETION_UNKNOWN, $userid = 0, $current = null) {
        global $USER, $DB;

        // Do nothing if completion is not enabled for that activity
        if (!$this->is_enabled($cm)) {
            return;
        }

        // Get current value of completion state and do nothing if it's same as
        // the possible result of this change. If the change is to COMPLETE and the
        // current value is one of the COMPLETE_xx subtypes, ignore that as well
        if (empty($current)) {
            $current = $this->get_data($cm, false, $userid);
        }
        if ($possibleresult == $current->completionstate ||
            ($possibleresult == COMPLETION_COMPLETE &&
                ($current->completionstate == COMPLETION_COMPLETE_PASS ||
                $current->completionstate == COMPLETION_COMPLETE_FAIL))) {
            return;
        }

        if ($cm->completion == COMPLETION_TRACKING_MANUAL) {
            // For manual tracking we set the result directly
            switch($possibleresult) {
                case COMPLETION_COMPLETE:
                case COMPLETION_INCOMPLETE:
                    $newstate = $possibleresult;
                    break;
                case COMPLETION_UNKNOWN:
                    // We cannot determine the state, so just use the current one :P
                    $newstate = $current->completionstate;
                    break;
                default:
                    $this->internal_systemerror("Unexpected manual completion state for {$cm->id}: $possibleresult");
            }

        } else {
            // Automatic tracking; get new state
            $newstate = $this->internal_get_state($cm, $userid, $current);
        }

        // If changed, update
        if ($newstate != $current->completionstate) {
            $current->completionstate = $newstate;
            $current->timemodified    = time();
            // If module_get_completion_state set time of completion then use it.
            if (isset($cm->timecompleted)) {
                $current->timecompleted = ($newstate == COMPLETION_INCOMPLETE) ? null : $cm->timecompleted;
            }
            // TOTARA - Whether or not this was 0 before, it should be 0 now as no reaggregation is necessary after this.
            $current->reaggregate = 0;
            $this->internal_set_data($cm, $current);
        }

        // Notify course completion.
        if (in_array($newstate, array(COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS, COMPLETION_COMPLETE_FAIL))) {
            $userid = $userid ? $userid : $USER->id;
            $event = \totara_core\event\module_completion::create(
                array(
                    'other' => array(
                            'moduleinstance' => $cm->id,
                            'userid' => $userid,
                            'course' => $this->course->id,
                            'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY,
                            'module' => $DB->get_field('modules', 'name', array('id' => $cm->module)),
                            ),
                )
            );
            $event->trigger();
        }
    }

    /**
     * Calculates the completion state for an activity and user.
     *
     * Internal function. Not private, so we can unit-test it.
     *
     * @param stdClass|cm_info $cm Activity
     * @param int $userid ID of user
     * @param stdClass $current Previous completion information from database
     * @return mixed
     */
    public function internal_get_state($cm, $userid, $current) {
        global $USER, $DB, $CFG;

        // Get user ID
        if (!$userid) {
            $userid = $USER->id;
        }

        // Check viewed
        if ($cm->completionview == COMPLETION_VIEW_REQUIRED &&
            $current->viewed == COMPLETION_NOT_VIEWED) {

            return COMPLETION_INCOMPLETE;
        }

        // Modname hopefully is provided in $cm but just in case it isn't, let's grab it
        if (!isset($cm->modname)) {
            // If we are here then it cannot possibly be a cm_info class.
            // We need to populate the modname property so that this looks like a cm_info class...
            // at least enough to to work here :(
            $cm->modname = $DB->get_field('modules', 'name', array('id'=>$cm->module));
        }

        $newstate = COMPLETION_COMPLETE;

        // Check grade
        if (!is_null($cm->completiongradeitemnumber)) {
            require_once($CFG->libdir.'/gradelib.php');
            $item = grade_item::fetch(array('courseid'=>$cm->course, 'itemtype'=>'mod',
                'itemmodule'=>$cm->modname, 'iteminstance'=>$cm->instance,
                'itemnumber'=>$cm->completiongradeitemnumber));
            if ($item) {
                // Fetch 'grades' (will be one or none)
                $grades = grade_grade::fetch_users_grades($item, array($userid), false);
                if (empty($grades)) {
                    // No grade for user
                    return COMPLETION_INCOMPLETE;
                }
                if (count($grades) > 1) {
                    $this->internal_systemerror("Unexpected result: multiple grades for
                        item '{$item->id}', user '{$userid}'");
                }
                $newstate = self::internal_get_grade_state($item, reset($grades));
                if ($newstate == COMPLETION_INCOMPLETE) {
                    return COMPLETION_INCOMPLETE;
                }

            } else {
                $this->internal_systemerror("Cannot find grade item for '{$cm->modname}'
                    cm '{$cm->id}' matching number '{$cm->completiongradeitemnumber}'");
            }
        }

        if (plugin_supports('mod', $cm->modname, FEATURE_COMPLETION_HAS_RULES)) {
            $function = $cm->modname.'_get_completion_state';
            if (!function_exists($function)) {
                $this->internal_systemerror("Module {$cm->modname} claims to support
                    FEATURE_COMPLETION_HAS_RULES but does not have required
                    {$cm->modname}_get_completion_state function");
            }
            if (!$function($this->course, $cm, $userid, COMPLETION_AND)) {
                return COMPLETION_INCOMPLETE;
            }
        }

        return $newstate;

    }

    /**
     * Marks a module as viewed.
     *
     * Should be called whenever a module is 'viewed' (it is up to the module how to
     * determine that). Has no effect if viewing is not set as a completion condition.
     *
     * Note that this function must be called before you print the page header because
     * it is possible that the navigation block may depend on it. If you call it after
     * printing the header, it shows a developer debug warning.
     *
     * @param stdClass|cm_info $cm Activity
     * @param int $userid User ID or 0 (default) for current user
     * @return void
     */
    public function set_module_viewed($cm, $userid=0) {
        global $PAGE;
        if ($PAGE->headerprinted) {
            debugging('set_module_viewed must be called before header is printed',
                    DEBUG_DEVELOPER);
        }

        // Don't do anything if view condition is not turned on
        if ($cm->completionview == COMPLETION_VIEW_NOT_REQUIRED || !$this->is_enabled($cm)) {
            return;
        }

        // Get current completion state
        $data = $this->get_data($cm, false, $userid);

        // If we already viewed it, don't do anything
        if ($data->viewed == COMPLETION_VIEWED) {
            return;
        }

        // OK, change state, save it, and update completion
        $data->viewed = COMPLETION_VIEWED;
        $this->internal_set_data($cm, $data);
        $this->update_state($cm, COMPLETION_COMPLETE, $userid);
    }

    /**
     * Resets a module as not viewed
     *
     * Should be called whenever a module is 'viewed' (it is up to the module how to
     * determine that). Has no effect if viewing is not set as a completion condition.
     *
     * Note that this function must be called before you print the page header because
     * it is possible that the navigation block may depend on it. If you call it after
     * printing the header, it shows a developer debug warning.
     *
     * @param stdClass|cm_info $cm Activity
     * @param int $userid User ID or 0 (default) for current user
     * @return void
     */
    public function set_module_viewed_reset($cm, $userid=0) {
        global $PAGE;
        if ($PAGE->headerprinted) {
            debugging('set_module_viewed must be called before header is printed',
                    DEBUG_DEVELOPER);
        }

        // Don't do anything if view condition is not turned on
        if ($cm->completionview == COMPLETION_VIEW_NOT_REQUIRED || !$this->is_enabled($cm)) {
            return;
        }

        // Get current completion state
        $data = $this->get_data($cm, false, $userid);

        // If we haven't already viewed it, don't do anything
        if ($data->viewed == COMPLETION_NOT_VIEWED) {
            return;
        }

        // OK, change state, save it, and update completion
        $data->viewed = COMPLETION_NOT_VIEWED;
        $this->internal_set_data($cm, $data);
        $this->update_state($cm, COMPLETION_INCOMPLETE, $userid);
    }

    /**
     * Determines how much completion data exists for an activity. This is used when
     * deciding whether completion information should be 'locked' in the module
     * editing form.
     *
     * @param cm_info $cm Activity
     * @return int The number of users who have completion data stored for this
     *   activity, 0 if none
     */
    public function count_user_data($cm) {
        global $DB;

        return $DB->get_field_sql("
    SELECT
        COUNT(1)
    FROM
        {course_modules_completion}
    WHERE
        coursemoduleid=? AND completionstate<>0", array($cm->id));
    }

    /**
     * Determines how much course completion criteria data exists for a course. This is used
     * to help decide whether completion information should be 'locked' in the completion
     * settings form and activity completion settings.
     *
     * @param int $user_id Optionally only get course completion data for a single user
     * @param bool $countrpl Optionally whether to count records completed via RPL
     * @return int The number of users who have completion data stored for this
     *     course, 0 if none
     */
    public function count_course_user_data($user_id = null, $countrpl = true) {
        global $DB;

        $params = array('courseid' => $this->course_id);

        // When deleting course completion data(delete_course_completion_data) we are ignoring records marked via RPL.
        // We need to excludes record where the course has been completed via RPL because these courses would not be
        // affected by completion criteria reset, so they shouldn't prevent it from occurring.
        $coursecompletionjoinsql = "";
        $coursecompletionwheresql = "";
        if (empty($user_id) && !$countrpl) {
            $coursecompletionjoinsql =
                "LEFT JOIN {course_completions} cc
                   ON cc.userid = cccc.userid AND cc.course = cccc.course AND status = :status";
            $coursecompletionwheresql =
                "AND cc.id IS NULL";
            $params['status'] = COMPLETION_STATUS_COMPLETEVIARPL;
        }

        $sql = "SELECT COUNT(1)
                  FROM {course_completion_crit_compl} cccc
                       {$coursecompletionjoinsql}
                 WHERE cccc.course = :courseid
                       {$coursecompletionwheresql}";

        if (!$countrpl) {
            $sql .= " AND (cccc.rpl = '' OR cccc.rpl IS NULL)";
        }

        // Limit data to a single user if an ID is supplied.
        if ($user_id) {
            $sql .= " AND cccc.userid = :userid";
            $params['userid'] = $user_id;
        }

        return $DB->get_field_sql($sql, $params);
    }

    /**
     * Determines how many course completion records exist for a course. This is used
     * to help decide whether completion information should be 'locked' in the completion
     * settings form and activity completion settings.
     *
     * @param int $user_id Optionally only get course completion records for a single user
     * @param bool $countrpl Optionally whether to count records completed via RPL
     * @return int The number of users who have completion records stored for this
     *     course, 0 if none
     */
    public function count_course_completions_data($user_id = null, $countrpl = true) {
        global $DB;

        $sql = '
    SELECT
        COUNT(1)
    FROM
        {course_completions}
    WHERE
        course = ?
        ';

        if ($countrpl == false) {
            $sql .= ' AND (rpl = \'\' OR rpl IS NULL)';
        }
        $params = array($this->course_id);

        // Limit data to a single user if an ID is supplied
        if ($user_id) {
            $sql .= ' AND userid = ?';
            $params[] = $user_id;
        }

        return $DB->get_field_sql($sql, $params);
    }


    /**
     * Check if this course's completion criteria should be locked
     *
     * If there is existing criteria completion data *or* any
     * course completion records then it should be locked as they
     * will be deleted when an unlocked form is saved.
     *
     * @param bool $countrpl count records completed via RPL
     * @return boolean
     */
    public function is_course_locked($countrpl = true) {
        return (bool) $this->count_course_user_data(null, $countrpl);
    }

    /**
     * Deletes all completion records for this course, including RPL.
     */
    public function delete_course_completion_data_including_rpl() {
        global $DB;

        $transaction = $DB->start_delegated_transaction();

        $DB->delete_records('course_completions', array('course' => $this->course_id));
        $DB->delete_records('course_completion_crit_compl', array('course' => $this->course_id));
        $DB->delete_records('block_totara_stats', array('eventtype' => STATS_EVENT_COURSE_STARTED, 'data2' => $this->course_id));
        $DB->delete_records('block_totara_stats', array('eventtype' => STATS_EVENT_COURSE_COMPLETE, 'data2' => $this->course_id));
        \core_completion\helper::save_completion_log($this->course_id, null,
            "Deleted current completion and all crit compl records in delete_course_completion_data_including_rpl");

        $transaction->allow_commit();

        // Difficult to find affected users, just purge all completion cache.
        $this->get_completion_cache()->purge();
        // Purge progressinfo cache also
        $this->get_progressinfo_cache()->purge();
    }

    /**
     * Deletes all course completion completion data.
     *
     * When called without a userid, course completion data will be deleted, EXCLUDING RPL completion related records.
     * When called with a userid, course completion data will be deleted, INCLUDING RPL completion related records.
     *
     * @param int $userid If provided, all course completion data for only this user and course will be deleted,
     *                    including RPL course and activity records.
     */
    public function delete_course_completion_data($userid = null) {
        global $DB, $CFG;
        require_once("{$CFG->dirroot}/blocks/totara_stats/locallib.php");

        $transaction = $DB->start_delegated_transaction();

        $criteriasql =
            "DELETE
               FROM {course_completion_crit_compl}
              WHERE course = :courseid";
        $statssql =
            "DELETE
               FROM {block_totara_stats}
              WHERE data2 = :courseid
                AND eventtype IN (:eventstarted, :eventcomplete)";
        $coursesql =
            "DELETE
               FROM {course_completions}
              WHERE course = :courseid";
        $params = array('courseid' => $this->course_id,
            'eventstarted' => STATS_EVENT_COURSE_STARTED,
            'eventcomplete' => STATS_EVENT_COURSE_COMPLETE);

        if (empty($userid)) {
            // Exclude deleting records for users who have RPL completions for this course.
            $criteriasql .= // Do not remove RPL activity completion records.
                " AND (rpl = '' OR rpl IS NULL)
                  AND NOT EXISTS (SELECT 1
                                   FROM {course_completions} cc
                                  WHERE cc.userid = {course_completion_crit_compl}.userid
                                    AND cc.course = {course_completion_crit_compl}.course
                                    AND cc.status = :status)";
            $statssql .=
                " AND NOT EXISTS (SELECT 1
                                   FROM {course_completions} cc
                                  WHERE cc.userid = {block_totara_stats}.userid
                                    AND cc.course = {block_totara_stats}.data2
                                    AND cc.status = :status)";
            $coursesql .= " AND status <> :status";
            $params['status'] = COMPLETION_STATUS_COMPLETEVIARPL;
            $message = "Deleted current completion and all crit compl records except where the current completion was RPL in delete_course_completion_data";
        } else {
            // Just delete the records for the specified user, even if they have a matching course RPL record.
            $criteriasql .= " AND {course_completion_crit_compl}.userid = :userid";
            $statssql .= " AND {block_totara_stats}.userid = :userid";
            $coursesql .= " AND {course_completions}.userid = :userid";
            $params['userid'] = $userid;
            $message = "Deleted current completion and all crit compl records in delete_course_completion_data";
        }

        $DB->execute($criteriasql, $params);
        $DB->execute($statssql, $params);
        // Do course_completions last, as the other two may depend on these records.
        $DB->execute($coursesql, $params);

        \core_completion\helper::save_completion_log($this->course_id, $userid, $message);

        $transaction->allow_commit();

        $cache = $this->get_completion_cache();
        if (empty($userid)) {
            // Difficult to find affected users, just purge all completion and progressinfo caches.
            $cache->purge();
            $this->get_progressinfo_cache()->purge();
        } else {
            $key = $userid . '_' . $this->course->id;
            $cache->delete($key);

            $this->mark_progressinfo_stale($userid);
        }
        // Difficult to find affected users, just purge all completion cache.
        cache::make('core', 'coursecompletion')->purge();

    }

    /**
     * Deletes all activity and course completion data for an entire course
     * (the below delete_all_state function does this for a single activity).
     *
     * Used by course reset page.
     */
    public function delete_all_completion_data() {
        global $DB;

        $transaction = $DB->start_delegated_transaction();

        // Delete from database.
        $DB->delete_records_select('course_modules_completion',
                'coursemoduleid IN (SELECT id FROM {course_modules} WHERE course=?)',
                array($this->course_id));
        \core_completion\helper::save_completion_log($this->course_id, null, "Deleted all module completions in delete_all_completion_data");

        // Wipe course completion data too.
        $this->delete_course_completion_data_including_rpl();

        $transaction->allow_commit();
    }

    /**
     * Deletes completion state related to an activity for all users.
     *
     * Intended for use only when the activity itself is deleted.
     *
     * @param stdClass|cm_info $cm Activity
     */
    public function delete_all_state($cm) {
        global $DB, $USER;

        $now = time();

        $transaction = $DB->start_delegated_transaction();

        // Delete from database
        $description = $DB->sql_concat(
            "'Deleted module completion in unused function delete_all_state<br><ul><li>CMCID: '",
            $DB->sql_cast_2char("cmc.id"),
            "'</li></ul>'"
        );
        $sql = "INSERT INTO {course_completion_log} (courseid, userid, changeuserid, description, timemodified)
                SELECT cm.course, cmc.userid, :changeuserid, {$description}, :now
                  FROM {course_modules_completion} cmc
                  JOIN {course_modules} cm
                    ON cm.id = cmc.coursemoduleid
                 WHERE coursemoduleid = :cmid";
        $DB->execute($sql, array('changeuserid' => $USER->id, 'now' => $now, 'cmid' => $cm->id));
        $DB->delete_records('course_modules_completion', array('coursemoduleid'=>$cm->id));

        // Check if there is an associated course completion criteria
        $criteria = $this->get_criteria(COMPLETION_CRITERIA_TYPE_ACTIVITY);
        $acriteria = false;
        foreach ($criteria as $criterion) {
            if ($criterion->moduleinstance == $cm->id) {
                $acriteria = $criterion;
                break;
            }
        }

        if ($acriteria) {
            // Delete all criteria completions relating to this activity, but skip any RPL records.
            $where = "course = :courseid AND criteriaid = :criteriaid AND (rpl = '' OR rpl IS NULL)";
            $params = array('courseid' => $this->course_id, 'criteriaid' => $acriteria->id);
            $description = $DB->sql_concat(
                "'Deleted crit compl in unused function delete_all_state<br><ul><li>CCCCID: '",
                $DB->sql_cast_2char("id"),
                "'</li></ul>'"
            );
            $sql = "INSERT INTO {course_completion_log} (courseid, userid, changeuserid, description, timemodified)
                SELECT course, userid, :changeuserid, {$description}, :now
                  FROM {course_completion_crit_compl}
                 WHERE {$where}";
            $logparams = array_merge(array('changeuserid' => $USER->id, 'now' => $now), $params);
            $DB->execute($sql, $logparams);
            $DB->delete_records_select('course_completion_crit_compl', $where, $params);

            $where = "course = :courseid AND (rpl = '' OR rpl IS NULL)";
            $params = array('courseid' => $this->course_id);
            $description = "'Deleted current completion in unused function delete_all_state'";
            $sql = "INSERT INTO {course_completion_log} (courseid, userid, changeuserid, description, timemodified)
                SELECT course, userid, :changeuserid, {$description}, :now
                  FROM {course_completions}
                 WHERE {$where}";
            $logparams = array_merge(array('changeuserid' => $USER->id, 'now' => $now), $params);
            $DB->execute($sql, $logparams);
            $DB->delete_records_select('course_completions', $where, $params);
        }

        $transaction->allow_commit();

        $this->get_completion_cache()->purge();
        // Difficult to find affected users, just purge all completion caches.
        cache::make('core', 'coursecompletion')->purge();
        $this->get_progressinfo_cache()->purge();
    }

    /**
     * Recalculates completion state related to an activity for all users.
     *
     * Intended for use if completion conditions change. (This should be avoided
     * as it may cause some things to become incomplete when they were previously
     * complete, with the effect - for example - of hiding a later activity that
     * was previously available.)
     *
     * Resetting state of manual tickbox has same result as deleting state for
     * it.
     *
     * @param stdClass|cm_info $cm Activity
     */
    public function reset_all_state($cm) {
        global $DB;

        if ($cm->completion == COMPLETION_TRACKING_MANUAL) {
            $this->delete_all_state($cm);
            return;
        }
        // Get current list of users with completion state
        $rs = $DB->get_recordset('course_modules_completion', array('coursemoduleid'=>$cm->id), '', 'userid');
        $keepusers = array();
        foreach ($rs as $rec) {
            $keepusers[] = $rec->userid;
        }
        $rs->close();

        // Delete all existing state [also clears MUC cache for current user]
        $this->delete_all_state($cm);

        // Merge this with list of planned users (according to roles)
        $trackedusers = $this->get_tracked_users();
        foreach ($trackedusers as $trackeduser) {
            $keepusers[] = $trackeduser->id;
        }
        $keepusers = array_unique($keepusers);

        // Recalculate state for each kept user. This function is not performed in course completion reset due to
        // possible performance problems, and instead it shows a notification and does the reaggregation on the
        // next cron run. This function can probably be safely removed and a notification added, and the following
        // call to completion_start_user_bulk will mark users as needing reaggregation.
        foreach ($keepusers as $keepuser) {
            $this->update_state($cm, COMPLETION_UNKNOWN, $keepuser);
        }

        // Bulk start users (creates missing course_completion records for all active participants).
        completion_start_user_bulk($cm->course);
    }

    /**
     * Returns the core completion cache instance.
     *
     * @return cache_application
     */
    protected function get_completion_cache() {
        return cache::make('core', 'completion');
    }

    /**
     * Returns the progressinfo cache.
     * @since Totara 10
     * @return cache_application
     */
    private function get_progressinfo_cache() {
        return cache::make('totara_core', 'completion_progressinfo');
    }

    /**
     * Returns the string to use for progressinfo cache keys.
     * @since Totara 10
     * @param int $userid
     * @return string
     */
    private function get_progressinfo_cache_key($userid) {
        return "{$this->course_id}_{$userid}";
    }

    /**
     * Marks the progressinfo cache stale for this entry
     * @since Totara 10
     * @param int $userid
     */
    public function mark_progressinfo_stale($userid) {
        $cache = $this->get_progressinfo_cache();
        $key = $this->get_progressinfo_cache_key($userid);
        $cache->delete($key);
    }

    /**
     * Obtains completion data for a particular activity and user (from the
     * MUC cache if available, or by SQL query)
     *
     * @param stdClass|cm_info $cm Activity; only required field is ->id
     * @param bool $wholecourse If true (default false) then, when necessary to
     *   fill the cache, retrieves information from the entire course not just for
     *   this one activity
     * @param int $userid User ID or 0 (default) for current user
     * @param array $modinfo Supply the value here - this is used for unit
     *   testing and so that it can be called recursively from within
     *   get_fast_modinfo. (Needs only list of all CMs with IDs.)
     *   Otherwise the method calls get_fast_modinfo itself.
     * @return stdClass Completion data (record from course_modules_completion)
     */
    public function get_data($cm, $wholecourse = false, $userid = 0, $modinfo = null) {
        global $USER, $DB;

        $completioncache = $this->get_completion_cache();

        // Get user ID
        if (!$userid) {
            $userid = $USER->id;
        }

        // See if requested data is present in cache (use cache for current user only).
        $usecache = $userid==$USER->id;
        $cacheddata = array();
        if ($usecache) {
            $key = $userid . '_' . $this->course->id;
            if (!isset($this->course->cacherev)) {
                $this->course = get_course($this->course_id);
            }
            $cacheddata = $completioncache->get($key);
            if ($cacheddata) {
                if ($cacheddata['cacherev'] != $this->course->cacherev) {
                    // Course structure has been changed since the last caching, forget the cache.
                    $cacheddata = array();
                } else if (isset($cacheddata[$cm->id])) {
                    return (object)$cacheddata[$cm->id];
                }
            } else {
                // cache::get returns false if the key is not found.
                // we need to convert that back to an empty array.
                $cacheddata = [];
            }
        }

        // Not there, get via SQL
        if ($usecache && $wholecourse) {
            // Get whole course data for cache
            $sql = "SELECT cmc.*
                      FROM {course_modules} cm
                INNER JOIN {course_modules_completion} cmc ON cmc.coursemoduleid=cm.id
                     WHERE cm.course=:courseid AND cmc.userid=:userid";
            $params = [
                'courseid' => $this->course->id,
                'userid' => $userid
            ];
            $alldatabycmc = $DB->get_records_sql($sql, $params);

            // Reindex by cm id
            $alldata = array();
            foreach ($alldatabycmc as $data) {
                $alldata[$data->coursemoduleid] = (array)$data;
            }

            // Get the module info and build up condition info for each one
            if (empty($modinfo)) {
                $modinfo = get_fast_modinfo($this->course, $userid);
            }
            foreach ($modinfo->cms as $othercm) {
                if (isset($alldata[$othercm->id])) {
                    $data = $alldata[$othercm->id];
                } else {
                    $data = (array)$this->get_incomplete_data_object($othercm->id, $userid);
                }
                $cacheddata[$othercm->id] = $data;
            }

            if (!isset($cacheddata[$cm->id])) {
                $this->internal_systemerror("Unexpected error: course-module {$cm->id} could not be found on course {$this->course->id}");
            }

        } else {
            // Get single record
            $data = $DB->get_record('course_modules_completion', array('coursemoduleid'=>$cm->id, 'userid'=>$userid));
            if ($data) {
                $data = (array)$data;
            } else {
                $data = (array)$this->get_incomplete_data_object($cm->id, $userid);
            }
            $cacheddata[$cm->id] = $data;
        }

        if ($usecache) {
            $cacheddata['cacherev'] = $this->course->cacherev;
            $completioncache->set($key, $cacheddata);
        }
        return (object)$cacheddata[$cm->id];
    }

    /**
     * Returns an course_modules_completion data object to represent an incomplete when the user has no current state and
     * can only be incomplete.
     *
     * @param int $cmid
     * @param int $userid
     * @return stdClass An incomplete course_modules_completion record.
     */
    protected function get_incomplete_data_object($cmid, $userid) {
        global $DB;

        // This needs to match a record from course_modules_completion.
        $data = new stdClass;
        $data->id = 0;
        $data->coursemoduleid = $cmid;
        $data->userid = $userid;
        $data->completionstate = COMPLETION_INCOMPLETE;
        $data->viewed = 0;
        $data->timemodified = 0;
        $data->timecompleted = null;
        $data->reaggregate = 0;

        // TOTARA - Check the criteria in case it should be complete.
        $sql = 'SELECT cm.*, m.name AS modname
                  FROM {course_modules} cm
                  JOIN {modules} m ON m.id = cm.module
                 WHERE cm.id = :cmid';
        $params = [
            'cmid' => $cmid
        ];
        $coursemodule = $DB->get_record_sql($sql, $params);
        if (!$coursemodule) {
            // The course module doesn't exist, this should not happen.
            // Handling this has been kept as legacy, but should be cleaned up in the future.
            debugging('Requesting completion data for a course module that does not exist', DEBUG_DEVELOPER);
            return $data;
        }

        $state = $this->internal_get_state($coursemodule, $userid, $data);
        if ($state != COMPLETION_INCOMPLETE) {
            // Its not incomplete, update the state so that we can return an accurate state.
            $this->update_state($coursemodule, COMPLETION_UNKNOWN, $userid, $data);
            $newdata = $DB->get_record('course_modules_completion', array('coursemoduleid' => $cmid, 'userid' => $userid));
            if (!empty($newdata)) {
                $data = $newdata;
            }
        }
        return $data;
    }

    /**
     * Invalidates the completion caches.
     * If both a userid and courseid are passed then the explicit cache entry is deleted.
     * Otherwise all completion cache and progressinfo cache entries are deleted.
     *
     * @param int $courseid
     * @param int $userid
     * @param bool $unused
     */
    public function invalidatecache($courseid = null, $userid = null, $unused = null) {
        $cache = $this->get_completion_cache();
        if (isset($courseid) && isset($userid)) {
            $key = $userid . '_' . $this->course->id;
            $cache->delete($key);
            // Invalidate progressinfo_cache entry as well
            $this->mark_progressinfo_stale($userid);
        } else {
            $cache->purge();
            $this->get_progressinfo_cache()->purge();
        }
    }

    /**
     * Updates completion data for a particular coursemodule and user (user is
     * determined from $data).
     *
     * (Internal function. Not private, so we can unit-test it.)
     *
     * @param stdClass|cm_info $cm Activity
     * @param stdClass $data Data about completion for that user
     */
    public function internal_set_data($cm, $data) {
        global $USER, $DB;

        $transaction = $DB->start_delegated_transaction();
        if (!$data->id) {
            // Check there isn't really a row
            $data->id = $DB->get_field('course_modules_completion', 'id',
                    array('coursemoduleid'=>$data->coursemoduleid, 'userid'=>$data->userid));
        }
        if (!$data->id) {
            // Didn't exist before, needs creating
            $data->id = $DB->insert_record('course_modules_completion', $data);
            \core_completion\helper::log_course_module_completion($data->id, "Created module completion in internal_set_data");
        } else {
            // Has real (nonzero) id meaning that a database row exists, update
            $DB->update_record('course_modules_completion', $data);
            \core_completion\helper::log_course_module_completion($data->id, "Updated module completion in internal_set_data");
        }
        $transaction->allow_commit();

        $cmcontext = context_module::instance($data->coursemoduleid, MUST_EXIST);

        $completioncache = $this->get_completion_cache();
        if ($data->userid == $USER->id) {
            // Update module completion in user's cache.
            $cachedata = $completioncache->get($data->userid . '_' . $cm->course);
            if (!$cachedata || $cachedata['cacherev'] != $this->course->cacherev) {
                // There is no cache data or the cacherev doesn't match and we need to reset.
                $cachedata = [
                    'cacherev' => $this->course->cacherev
                ];
            }
            $cachedata[$cm->id] = $data;
            $completioncache->set($data->userid . '_' . $cm->course, $cachedata);
            // reset modinfo for user (no need to call rebuild_course_cache())
            get_fast_modinfo($cm->course, 0, true);
        } else {
            // Remove another user's completion cache for this course.
            $completioncache->delete($data->userid . '_' . $cm->course);
        }
        $this->mark_progressinfo_stale($data->userid);

        // Trigger an event for course module completion changed.
        $event = \core\event\course_module_completion_updated::create(array(
            'objectid' => $data->id,
            'context' => $cmcontext,
            'relateduserid' => $data->userid,
            'other' => array(
                'relateduserid' => $data->userid
            )
        ));
        $event->add_record_snapshot('course_modules_completion', $data);
        $event->trigger();
    }

     /**
     * Return whether or not the course has activities with completion enabled.
     *
     * @return boolean true when there is at least one activity with completion enabled.
     */
    public function has_activities() {
        $modinfo = get_fast_modinfo($this->course);
        foreach ($modinfo->get_cms() as $cm) {
            if ($cm->completion != COMPLETION_TRACKING_NONE) {
                return true;
            }
        }
        return false;
    }

    /**
     * Obtains a list of activities for which completion is enabled on the
     * course. The list is ordered by the section order of those activities.
     *
     * @param array $modinfo For unit testing only, supply the value
     *   here. Otherwise the method calls get_fast_modinfo
     * @return cm_info[] Array from $cmid => $cm of all activities with completion enabled,
     *   empty array if none
     */
    public function get_activities($modinfo = null) {

        if ($modinfo === null) {
            $modinfo = get_fast_modinfo($this->course);
        } else {
            if (!PHPUNIT_TEST) {
                $modinfo = get_fast_modinfo($this->course);
                debugging('get_activities() parameter $modinfo can be used in unit tests only!');
            }
        }

        $result = array();
        foreach ($modinfo->get_cms() as $cm) {
            if ($cm->completion != COMPLETION_TRACKING_NONE && !$cm->deletioninprogress) {
                $result[$cm->id] = $cm;
            }
        }

        return $result;
    }

    /**
     * Checks to see if the userid has a completion_completions record
     * or a tracked role in this course.
     *
     * We do this because some users could have received a RPL via
     * a Learning Plan without being enrolled in the course at all.
     *
     * @param int $userid User id
     * @return bool
     */
    public function is_tracked_user($userid) {
        return is_enrolled(context_course::instance($this->course->id), $userid, 'moodle/course:isincompletionreports', true);
    }

    /**
     * Returns the number of users whose progress is tracked in this course.
     *
     * Optionally supply a search's where clause, or a group id.
     *
     * @param string $where Where clause sql (use 'u.whatever' for user table fields)
     * @param array $whereparams Where clause params
     * @param int $groupid Group id
     * @param bool $activeonly consider only active enrolments in enabled plugins and time restrictions
     * @return int Number of tracked users
     */
    public function get_num_tracked_users($where = '', $whereparams = array(), $groupid = 0, $activeonly = true) {
        global $DB;

        list($enrolledsql, $enrolledparams) = get_enrolled_sql(
                context_course::instance($this->course->id), 'moodle/course:isincompletionreports', $groupid, $activeonly);
        $sql  = 'SELECT COUNT(eu.id) FROM (' . $enrolledsql . ') eu JOIN {user} u ON u.id = eu.id';
        if ($where) {
            $sql .= " WHERE $where";
        }

        $params = array_merge($enrolledparams, $whereparams);
        return $DB->count_records_sql($sql, $params);
    }

    /**
     * Return array of users whose progress is tracked in this course.
     *
     * Optionally supply a search's where clause, group id, sorting, paging.
     *
     * @param string $where Where clause sql, referring to 'u.' fields (optional)
     * @param array $whereparams Where clause params (optional)
     * @param int $groupid Group ID to restrict to (optional)
     * @param string $sort Order by clause (optional)
     * @param int $limitfrom Result start (optional)
     * @param int $limitnum Result max size (optional)
     * @param context $extracontext If set, includes extra user information fields
     *   as appropriate to display for current user in this context
     * @param bool $activeonly consider only active enrolments in enabled plugins and time restrictions
     * @return array Array of user objects with standard user fields
     */
    public function get_tracked_users($where = '', $whereparams = array(), $groupid = 0,
             $sort = '', $limitfrom = '', $limitnum = '', context $extracontext = null, $activeonly = true) {

        global $DB;

        list($enrolledsql, $params) = get_enrolled_sql(
                context_course::instance($this->course->id),
                'moodle/course:isincompletionreports', $groupid, $activeonly);

        $allusernames = get_all_user_name_fields(true, 'u');
        $sql = 'SELECT u.id, u.idnumber, ' . $allusernames;
        if ($extracontext) {
            $sql .= get_extra_user_fields_sql($extracontext, 'u', '', array('idnumber'));
        }
        $sql .= ' FROM (' . $enrolledsql . ') eu JOIN {user} u ON u.id = eu.id';

        if ($where) {
            $sql .= " AND $where";
            $params = array_merge($params, $whereparams);
        }

        if ($sort) {
            $sql .= " ORDER BY $sort";
        }

        return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
    }

    /**
     * Obtains progress information across a course for all users on that course, or
     * for all users in a specific group. Intended for use when displaying progress.
     *
     * This includes only users who, in course context, have one of the roles for
     * which progress is tracked (the gradebookroles admin option) and are enrolled in course.
     *
     * Users are included (in the first array) even if they do not have
     * completion progress for any course-module.
     *
     * @param bool $sortfirstname If true, sort by first name, otherwise sort by
     *   last name
     * @param string $where Where clause sql (optional)
     * @param array $where_params Where clause params (optional)
     * @param int $groupid Group ID or 0 (default)/false for all groups
     * @param int $pagesize Number of users to actually return (optional)
     * @param int $start User to start at if paging (optional)
     * @param context $extracontext If set, includes extra user information fields
     *   as appropriate to display for current user in this context
     * @param bool $activeonly consider only active enrolments in enabled plugins and time restrictions
     * @return stdClass with ->total and ->start (same as $start) and ->users;
     *   an array of user objects (like mdl_user id, firstname, lastname)
     *   containing an additional ->progress array of coursemoduleid => completionstate
     */
    public function get_progress_all($where = '', $where_params = array(), $groupid = 0,
            $sort = '', $pagesize = '', $start = '', context $extracontext = null, $activeonly = true) {
        global $CFG, $DB;

        // Get list of applicable users
        $users = $this->get_tracked_users($where, $where_params, $groupid, $sort,
                $start, $pagesize, $extracontext, $activeonly);

        // Get progress information for these users in groups of 1, 000 (if needed)
        // to avoid making the SQL IN too long
        $results = array();
        $userids = array();
        foreach ($users as $user) {
            $userids[] = $user->id;
            $results[$user->id] = $user;
            $results[$user->id]->progress = array();
        }

        for($i=0; $i<count($userids); $i+=1000) {
            $blocksize = count($userids)-$i < 1000 ? count($userids)-$i : 1000;

            list($insql, $params) = $DB->get_in_or_equal(array_slice($userids, $i, $blocksize));
            array_splice($params, 0, 0, array($this->course->id));
            $rs = $DB->get_recordset_sql("
                SELECT
                    cmc.*
                FROM
                    {course_modules} cm
                    INNER JOIN {course_modules_completion} cmc ON cm.id=cmc.coursemoduleid
                WHERE
                    cm.course=? AND cmc.userid $insql", $params);
            foreach ($rs as $progress) {
                $progress = (object)$progress;
                $results[$progress->userid]->progress[$progress->coursemoduleid] = $progress;
            }
            $rs->close();
        }

        return $results;
    }

    /**
     * Called by grade code to inform the completion system when a grade has
     * been changed. If the changed grade is used to determine completion for
     * the course-module, then the completion status will be updated.
     *
     * @param stdClass|cm_info $cm Course-module for item that owns grade
     * @param grade_item $item Grade item
     * @param stdClass $grade
     * @param bool $deleted
     */
    public function inform_grade_changed($cm, $item, $grade, $deleted) {
        // Bail out now if completion is not enabled for course-module, it is enabled
        // but is set to manual, grade is not used to compute completion, or this
        // is a different numbered grade
        if (!$this->is_enabled($cm) ||
            $cm->completion == COMPLETION_TRACKING_MANUAL ||
            is_null($cm->completiongradeitemnumber) ||
            $item->itemnumber != $cm->completiongradeitemnumber) {
            return;
        }

        // What is the expected result based on this grade?
        if ($deleted) {
            // Grade being deleted, so only change could be to make it incomplete
            $possibleresult = COMPLETION_INCOMPLETE;
        } else {
            $possibleresult = self::internal_get_grade_state($item, $grade);
        }

        // OK, let's update state based on this
        $this->update_state($cm, $possibleresult, $grade->userid);
    }

    /**
     * Calculates the completion state that would result from a graded item
     * (where grade-based completion is turned on) based on the actual grade
     * and settings.
     *
     * Internal function. Not private, so we can unit-test it.
     *
     * @param grade_item $item an instance of grade_item
     * @param grade_grade $grade an instance of grade_grade
     * @return int Completion state e.g. COMPLETION_INCOMPLETE
     */
    public static function internal_get_grade_state($item, $grade) {
        // If no grade is supplied or the grade doesn't have an actual value, then
        // this is not complete.
        if (!$grade || (is_null($grade->finalgrade) && is_null($grade->rawgrade))) {
            return COMPLETION_INCOMPLETE;
        }

        // Conditions to show pass/fail:
        // a) Grade has pass mark (default is 0.00000 which is boolean true so be careful)
        // b) Grade is visible (neither hidden nor hidden-until)
        if ($item->gradepass && $item->gradepass > 0.000009 && !$item->hidden) {
            // Use final grade if set otherwise raw grade
            $score = !is_null($grade->finalgrade) ? $grade->finalgrade : $grade->rawgrade;

            // We are displaying and tracking pass/fail
            if ($score >= $item->gradepass) {
                return COMPLETION_COMPLETE_PASS;
            } else {
                return COMPLETION_COMPLETE_FAIL;
            }
        } else {
            // Not displaying pass/fail, so just if there is a grade
            if (!is_null($grade->finalgrade) || !is_null($grade->rawgrade)) {
                // Grade exists, so maybe complete now
                return COMPLETION_COMPLETE;
            } else {
                // Grade does not exist, so maybe incomplete now
                return COMPLETION_INCOMPLETE;
            }
        }
    }

    /**
     * Aggregate activity completion state
     *
     * @param   int     $type   Aggregation type (COMPLETION_* constant)
     * @param   bool    $old    Old state
     * @param   bool    $new    New state
     * @return  bool
     */
    public static function aggregate_completion_states($type, $old, $new) {
        if ($type == COMPLETION_AND) {
            return $old && $new;
        } else {
            return $old || $new;
        }
    }

    /**
     * This is to be used only for system errors (things that shouldn't happen)
     * and not user-level errors.
     *
     * @global type $CFG
     * @param string $error Error string (will not be displayed to user unless debugging is enabled)
     * @throws moodle_exception Exception with the error string as debug info
     */
    public function internal_systemerror($error) {
        global $CFG;
        throw new moodle_exception('err_system','completion',
            $CFG->wwwroot.'/course/view.php?id='.$this->course->id,null,$error);
    }

    /**
     * DEPRECATED: For testing only.
     *
     * @deprecated since Totara 10, this function should no longer be needed for testing.
     */
    public static function wipe_session_cache() {
        debugging('The method \completion_info::wipe_session_cache has been deprecated and will be removed.', DEBUG_DEVELOPER);
        $cache = cache::make('core', 'completion');
        $cache->purge();
    }

    /**
     * Get all completions a user has across the site
     * @access  public
     * @param   $userid     int     User id
     * @param   $limit      int     Limit number of records returned (optional)
     * @return  array
     */
    public static function get_all_courses($userid, $limit=0) {
        global $DB;

        list($visibilitysql, $visibilityparams) = totara_visibility_where($userid, 'c.id', 'c.visible', 'c.audiencevisible');
        $params = array('userid' => $userid);
        $params = array_merge($params, $visibilityparams);

        $sql = "
            SELECT
                c.id AS course,
                c.fullname AS name,
                cc.timeenrolled,
                cc.timestarted,
                cc.timecompleted,
                cc.status,
                cc.rpl
            FROM
                {course_completions} cc
            LEFT JOIN
                {course} c
             ON cc.course = c.id
            LEFT JOIN
                {context} ctx
             ON ctx.instanceid = c.id AND ctx.contextlevel = " . CONTEXT_COURSE . "
            WHERE
                cc.userid = :userid
            AND {$visibilitysql}
            AND
            (
                cc.timeenrolled > 0
             OR cc.timestarted > 0
             OR cc.timecompleted > 0
            )
            ORDER BY
                cc.timeenrolled DESC,
                cc.timestarted DESC,
                cc.timecompleted DESC
        ";

        $completions = array();

        if ($ccompletions = $DB->get_records_sql($sql, $params, 0, $limit)) {
            foreach ($ccompletions as $course) {
                // Create completion_completion instance (without reloading from db)
                $completions[$course->course] = new completion_completion((array) $course, false);
            }
        }

        return $completions;
    }

    /**
     * TOTARA - Checks if a user has some form of completion status in a course.
     *
     * @param int $userid - the id of the user.
     * @return bool - true if there is a completion status and false if not.
     */
    public function user_has_completion_status($userid) {

        if (empty($userid)) {
            // Not logged in.
            return false;
        } else if (isguestuser($userid)) {
            // Guest account can not have completions.
            return false;
        }

        if ($this->is_tracked_user($userid)) {
            return true;
        }

        if ($this->count_course_user_data($userid)) {
            return true;
        }

        $ccompletion = new completion_completion(array('userid' => $userid, 'course' => $this->course_id));
        if ($ccompletion->timestarted) {
            return true;
        }

        return false;
    }


    /**
     * Build progress aggregation information hierarchy
     *
     * The root node represents the course.
     * The following level contains a node for each type of activity for which criteria
     * was specified.
     * Criteria types that may itself have multiple criteria (e.g. activities, courses, roles),
     * will have child nodes representing each individual criteria of the specific type.
     *
     * @since Totara 10
     */
    private function build_progressinfo() {
        global $CFG, $DB;

        $agg_method = \totara_core\progressinfo::AGGREGATE_ANY;
        if ($this->get_aggregation_method() == COMPLETION_AGGREGATION_ALL) {
            $agg_method = \totara_core\progressinfo::AGGREGATE_ALL;
        }
        $progressinfo = \totara_core\progressinfo::from_data($agg_method);

        if (!$this->is_enabled()) {
            $progressinfo->set_customdata(array('enabled' => false));
            return $progressinfo;
        }

        $multi_activity_criteria = self::get_multi_activity_criteria();

        $completioncriteria = $this->get_criteria();

        // Retrieve progress and weight for all completion criteria
        foreach ($completioncriteria as $criteria) {

            // For courses, customdata is added to allow us to get completion summary
            // information without the need to re-read the full completion structure
            $customdata = null;
            switch ($criteria->criteriatype) {

                case COMPLETION_CRITERIA_TYPE_DATE:
                    $format = get_string('strfdateshortmonth', 'langconfig');
                    $customdata = ['date' => userdate($criteria->timeend, $format, null, false)];
                    break;

                case COMPLETION_CRITERIA_TYPE_DURATION:
                    $customdata = ['duration' => get_string('xdays', 'completion', ceil($criteria->enrolperiod / (60*60*24)))];
                    break;

                case COMPLETION_CRITERIA_TYPE_GRADE:
                    $customdata = ['grade' => format_float($criteria->gradepass, $CFG->grade_decimalpoints)];
                    break;

                case COMPLETION_CRITERIA_TYPE_ROLE:
                    // Customdata for roles will eventually contain the names of all required roles - handled later in the function
                    $role = $DB->get_record('role', array('id' => $criteria->role));
                    if (!$role) {
                        $customdata = ['roles' => get_string('roleidnotfound', 'completion', $criteria->role)];
                    } else {
                        $customdata = ['roles' => role_get_name($role)];
                    }
                    break;
            }

            // Only add if this criteriatype not yet added
            $key = $criteria->criteriatype;

            if (!$progressinfo->criteria_exist($key)) {
                if (array_key_exists($key, $multi_activity_criteria)) {

                    $agg_method = \totara_core\progressinfo::AGGREGATE_ANY;
                    if ($this->get_aggregation_method($key) == COMPLETION_AGGREGATION_ALL) {
                        $agg_method = \totara_core\progressinfo::AGGREGATE_ALL;
                    }
                    $critinfo = $progressinfo->add_criteria($key, $agg_method, 0, 0, $customdata);

                    // unset customdata as it has already been added for types with multiple criteria
                    $customdata = null;
                } else {
                    // For single criteria - add to top level
                    $critinfo = $progressinfo;
                }
            } else {
                $critinfo = $progressinfo->get_criteria($key);
                // Update customdata for roles - other existing info would have customdata from time of creation
                if ($key == COMPLETION_CRITERIA_TYPE_ROLE) {
                    // Append this rolename to the customdata
                    $rolecustomdata = $critinfo->get_customdata();
                    // It should have a customdata['roles'], but handle all cases
                    if (isset($rolecustomdata['roles'])) {
                        $rolecustomdata['roles'] = $rolecustomdata['roles'] . ', ' . $customdata['roles'];
                    } else {
                        $rolecustomdata['roles'] = $customdata['roles'];
                    }
                    $critinfo->set_customdata($rolecustomdata);
                }

                $customdata = null;
            }

            if ($critinfo) {
                if (array_key_exists($criteria->criteriatype, $multi_activity_criteria)) {
                    $criteriatype = $multi_activity_criteria[$criteria->criteriatype];
                    $type = $criteria->{$criteriatype};
                } else {
                    $type = $criteria->criteriatype;;
                }

                if (!$critinfo->criteria_exist($type)) {
                    $agg_method = \totara_core\progressinfo::AGGREGATE_ALL;
                    $critinfo->add_criteria($type, $agg_method, $criteria->get_weight(), 0, $customdata);
                }
            }
        }

        return $progressinfo;
    }

    /**
     * Return the progress aggregation information structure
     *
     * @since Totara 10
     * @return \totara_core\progressinfo
     */
    public function get_progressinfo() {
        if (empty($this->progressinfo)) {
            $this->progressinfo = $this->build_progressinfo();
        }

        return $this->progressinfo;
    }
}
