Exemplo n.º 1
0
Arquivo: lib.php Projeto: ruddj/moodle
/**
 * Obtains the automatic completion state for this scorm based on any conditions
 * in scorm settings.
 *
 * @param object $course Course
 * @param object $cm Course-module
 * @param int $userid User ID
 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
 * @return bool True if completed, false if not. (If no conditions, then return
 *   value depends on comparison type)
 */
function scorm_get_completion_state($course, $cm, $userid, $type) {
    global $DB;

    $result = $type;

    // Get scorm.
    if (!$scorm = $DB->get_record('scorm', array('id' => $cm->instance))) {
        print_error('cannotfindscorm');
    }
    // Only check for existence of tracks and return false if completionstatusrequired or completionscorerequired
    // this means that if only view is required we don't end up with a false state.
    if ($scorm->completionstatusrequired !== null ||
        $scorm->completionscorerequired !== null) {
        // Get user's tracks data.
        $tracks = $DB->get_records_sql(
            "
            SELECT
                id,
                element,
                value
            FROM
                {scorm_scoes_track}
            WHERE
                scormid = ?
            AND userid = ?
            AND element IN
            (
                'cmi.core.lesson_status',
                'cmi.completion_status',
                'cmi.success_status',
                'cmi.core.score.raw',
                'cmi.score.raw'
            )
            ",
            array($scorm->id, $userid)
        );

        if (!$tracks) {
            return completion_info::aggregate_completion_states($type, $result, false);
        }
    }

    // Check for status.
    if ($scorm->completionstatusrequired !== null) {

        // Get status.
        $statuses = array_flip(scorm_status_options());
        $nstatus = 0;

        foreach ($tracks as $track) {
            if (!in_array($track->element, array('cmi.core.lesson_status', 'cmi.completion_status', 'cmi.success_status'))) {
                continue;
            }

            if (array_key_exists($track->value, $statuses)) {
                $nstatus |= $statuses[$track->value];
            }
        }

        if ($scorm->completionstatusrequired & $nstatus) {
            return completion_info::aggregate_completion_states($type, $result, true);
        } else {
            return completion_info::aggregate_completion_states($type, $result, false);
        }

    }

    // Check for score.
    if ($scorm->completionscorerequired !== null) {
        $maxscore = -1;

        foreach ($tracks as $track) {
            if (!in_array($track->element, array('cmi.core.score.raw', 'cmi.score.raw'))) {
                continue;
            }

            if (strlen($track->value) && floatval($track->value) >= $maxscore) {
                $maxscore = floatval($track->value);
            }
        }

        if ($scorm->completionscorerequired <= $maxscore) {
            return completion_info::aggregate_completion_states($type, $result, true);
        } else {
            return completion_info::aggregate_completion_states($type, $result, false);
        }
    }

    return $result;
}
Exemplo n.º 2
0
Arquivo: lib.php Projeto: dg711/moodle
/**
 * Obtains the automatic completion state for this scorm based on any conditions
 * in scorm settings.
 *
 * @param object $course Course
 * @param object $cm Course-module
 * @param int $userid User ID
 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
 * @return bool True if completed, false if not. (If no conditions, then return
 *   value depends on comparison type)
 */
function scorm_get_completion_state($course, $cm, $userid, $type)
{
    global $DB;
    $result = $type;
    // Get scorm.
    if (!($scorm = $DB->get_record('scorm', array('id' => $cm->instance)))) {
        print_error('cannotfindscorm');
    }
    // Only check for existence of tracks and return false if completionstatusrequired or completionscorerequired
    // this means that if only view is required we don't end up with a false state.
    if ($scorm->completionstatusrequired !== null || $scorm->completionscorerequired !== null) {
        // Get user's tracks data.
        $tracks = $DB->get_records_sql("\n            SELECT\n                id,\n                scoid,\n                element,\n                value\n            FROM\n                {scorm_scoes_track}\n            WHERE\n                scormid = ?\n            AND userid = ?\n            AND element IN\n            (\n                'cmi.core.lesson_status',\n                'cmi.completion_status',\n                'cmi.success_status',\n                'cmi.core.score.raw',\n                'cmi.score.raw'\n            )\n            ", array($scorm->id, $userid));
        if (!$tracks) {
            return completion_info::aggregate_completion_states($type, $result, false);
        }
    }
    // Check for status.
    if ($scorm->completionstatusrequired !== null) {
        // Get status.
        $statuses = array_flip(scorm_status_options());
        $nstatus = 0;
        // Check any track for these values.
        $scostatus = array();
        foreach ($tracks as $track) {
            if (!in_array($track->element, array('cmi.core.lesson_status', 'cmi.completion_status', 'cmi.success_status'))) {
                continue;
            }
            if (array_key_exists($track->value, $statuses)) {
                $scostatus[$track->scoid] = true;
                $nstatus |= $statuses[$track->value];
            }
        }
        if (!empty($scorm->completionstatusallscos)) {
            // Iterate over all scos and make sure each has a lesson_status.
            $scos = $DB->get_records('scorm_scoes', array('scorm' => $scorm->id, 'scormtype' => 'sco'));
            foreach ($scos as $sco) {
                if (empty($scostatus[$sco->id])) {
                    return completion_info::aggregate_completion_states($type, $result, false);
                }
            }
            return completion_info::aggregate_completion_states($type, $result, true);
        } else {
            if ($scorm->completionstatusrequired & $nstatus) {
                return completion_info::aggregate_completion_states($type, $result, true);
            } else {
                return completion_info::aggregate_completion_states($type, $result, false);
            }
        }
    }
    // Check for score.
    if ($scorm->completionscorerequired !== null) {
        $maxscore = -1;
        foreach ($tracks as $track) {
            if (!in_array($track->element, array('cmi.core.score.raw', 'cmi.score.raw'))) {
                continue;
            }
            if (strlen($track->value) && floatval($track->value) >= $maxscore) {
                $maxscore = floatval($track->value);
            }
        }
        if ($scorm->completionscorerequired <= $maxscore) {
            return completion_info::aggregate_completion_states($type, $result, true);
        } else {
            return completion_info::aggregate_completion_states($type, $result, false);
        }
    }
    return $result;
}
Exemplo n.º 3
0
 public function add_completion_rules()
 {
     $mform =& $this->_form;
     $items = array();
     // Require score.
     $group = array();
     $group[] =& $mform->createElement('text', 'completionscorerequired', '', array('size' => 5));
     $group[] =& $mform->createElement('checkbox', 'completionscoredisabled', null, get_string('disable'));
     $mform->setType('completionscorerequired', PARAM_INT);
     $mform->addGroup($group, 'completionscoregroup', get_string('completionscorerequired', 'scorm'), '', false);
     $mform->addHelpButton('completionscoregroup', 'completionscorerequired', 'scorm');
     $mform->disabledIf('completionscorerequired', 'completionscoredisabled', 'checked');
     $mform->setDefault('completionscorerequired', 0);
     $items[] = 'completionscoregroup';
     // Require status.
     $first = true;
     $firstkey = null;
     foreach (scorm_status_options(true) as $key => $value) {
         $name = null;
         $key = 'completionstatusrequired[' . $key . ']';
         if ($first) {
             $name = get_string('completionstatusrequired', 'scorm');
             $first = false;
             $firstkey = $key;
         }
         $mform->addElement('checkbox', $key, $name, $value);
         $mform->setType($key, PARAM_BOOL);
         $items[] = $key;
     }
     $mform->addHelpButton($firstkey, 'completionstatusrequired', 'scorm');
     return $items;
 }
Exemplo n.º 4
0
/**
 * Obtains the automatic completion state for this scorm based on any conditions
 * in scorm settings.
 *
 * @param object $course Course
 * @param object $cm Course-module
 * @param int $userid User ID
 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
 * @return bool True if completed, false if not. (If no conditions, then return
 *   value depends on comparison type)
 */
function scorm_get_completion_state($course, $cm, $userid, $type)
{
    global $DB;
    $result = $type;
    // Get scorm
    if (!($scorm = $DB->get_record('scorm', array('id' => $cm->instance)))) {
        print_error('cannotfindscorm');
    }
    // Get user's tracks data
    $tracks = $DB->get_records_sql("\n        SELECT\n            id,\n            element,\n            value\n        FROM\n            {scorm_scoes_track}\n        WHERE\n            scormid = ?\n        AND userid = ?\n        AND element IN\n        (\n            'cmi.core.lesson_status',\n            'cmi.completion_status',\n            'cmi.success_status',\n            'cmi.core.score.raw',\n            'cmi.score.raw'\n        )\n        ", array($scorm->id, $userid));
    if (!$tracks) {
        return completion_info::aggregate_completion_states($type, $result, false);
    }
    // Check for status
    if ($scorm->completionstatusrequired !== null) {
        // Get status
        $statuses = array_flip(scorm_status_options());
        $nstatus = 0;
        foreach ($tracks as $track) {
            if (!in_array($track->element, array('cmi.core.lesson_status', 'cmi.completion_status', 'cmi.success_status'))) {
                continue;
            }
            if (array_key_exists($track->value, $statuses)) {
                $nstatus |= $statuses[$track->value];
            }
        }
        if ($scorm->completionstatusrequired & $nstatus) {
            return completion_info::aggregate_completion_states($type, $result, true);
        } else {
            return completion_info::aggregate_completion_states($type, $result, false);
        }
    }
    // Check for score
    if ($scorm->completionscorerequired !== null) {
        $maxscore = -1;
        foreach ($tracks as $track) {
            if (!in_array($track->element, array('cmi.core.score.raw', 'cmi.score.raw'))) {
                continue;
            }
            if (strlen($track->value) && floatval($track->value) >= $maxscore) {
                $maxscore = floatval($track->value);
            }
        }
        if ($scorm->completionscorerequired <= $maxscore) {
            return completion_info::aggregate_completion_states($type, $result, true);
        } else {
            return completion_info::aggregate_completion_states($type, $result, false);
        }
    }
    return $result;
}