Ejemplo n.º 1
0
/**
 * Function to be run periodically according to the moodle cron
 * This function searches for things that need to be done, such
 * as sending out mail, toggling flags etc ...
 *
 * @return boolean
 * @todo Finish documenting this function
 **/
function studyplan_cron()
{
    global $DB;
    mtrace("\n  Studyplan Cron for progress updates", '');
    //get just the courses that have studyplans in them
    $courses = get_courses();
    $studyplans = get_all_instances_in_courses('studyplan', $courses);
    $courses_subset = array();
    $course_studyplans = array();
    foreach ($studyplans as $studyplan) {
        $courses_subset[] = $studyplan->course;
        if (!$course_studyplans[$studyplan->course]) {
            $course_studyplans[$studyplan->course] = array();
        }
        $course_studyplans[$studyplan->course][] = $studyplan->id;
    }
    //only do this is there are coursese with studyplan in them
    if ($courses_subset) {
        //find all the users that logged in today and are in the course subset
        $courses_subset_sql = "courseid in (" . join(",", $courses_subset) . ")";
        $now = time();
        $last_24_hrs = $now - 24 * 60 * 60;
        $last_7_hrs = $now - 7 * 60 * 60;
        //because cron got set to 6 hours
        #note: using left join on the progress table so that we'll get blanks to create
        #so check if progressid is null
        $sql = "SELECT {user_lastaccess}.*, {studyplan}.id as studyplanid, {studyplan_progress}.id as progressid, {studyplan_progress}.percent\n\t\t\tFROM {user_lastaccess}\n\t\t\tJOIN {studyplan}\n\t\t\tON {user_lastaccess}.courseid = {studyplan}.course\n\t\t\tLEFT JOIN {studyplan_progress}\n\t\t\tON ( {studyplan_progress}.id = {studyplan_progress}.studyplan\n\t\t\tAND {studyplan_progress}.user = {user_lastaccess}.userid )\n\t\t\tWHERE {user_lastaccess}.{$courses_subset_sql}\n\t\t\tAND {user_lastaccess}.timeaccess > {$last_7_hrs}\n\t\t";
        $results = $DB->get_records_sql($sql);
        mtrace("\n" . count($results) . ' students in last 7 hours.');
        if ($results !== false) {
            foreach ($results as $r) {
                sp_calculate_student_progress($r->studyplanid, $r->userid, true, false);
            }
        }
    }
    mtrace('done.');
}
Ejemplo n.º 2
0
$n = optional_param('n', 0, PARAM_INT);
// studyplan instance ID - it should be named as the first character of the module
if ($id) {
    $cm = get_coursemodule_from_id('studyplan', $id, 0, false, MUST_EXIST);
    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
    $studyplan = $DB->get_record('studyplan', array('id' => $cm->instance), '*', MUST_EXIST);
} elseif ($n) {
    $studyplan = $DB->get_record('studyplan', array('id' => $n), '*', MUST_EXIST);
    $course = $DB->get_record('course', array('id' => $studyplan->course), '*', MUST_EXIST);
    $cm = get_coursemodule_from_instance('studyplan', $studyplan->id, $course->id, false, MUST_EXIST);
} else {
    error(get_string('specifyiderror', 'studyplan'));
}
if ($studentid) {
    $STUDENT = $DB->get_record('user', array('id' => $studentid), '*', MUST_EXIST);
}
require_login($course, true, $cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/studyplan:assign', $context);
add_to_log($course->id, 'studyplan', 'assign', "assign.php?id={$cm->id}&student={$studentid}&block={$blockid}", $studyplan->name, $cm->id);
// Print the page header
$PAGE->set_cacheable(false);
sp_toggle_block_assigned($blockid, $studyplan->id, $studentid);
$percent = sp_calculate_student_progress($studyplan->id, $studentid);
?>
/* SUCCESS - now set the progress percentage */
(function() { Y.one('.studyplan-progress-percent').set('text','<?php 
echo $percent;
?>
'); })()