Beispiel #1
0
function checklist_update_all_grades()
{
    global $DB;
    $checklists = $DB->get_records('checklist');
    foreach ($checklists as $checklist) {
        checklist_update_grades($checklist);
    }
}
function checklist_completion_autoupdate($cmid, $userid, $newstate)
{
    global $DB, $CFG, $USER;
    if ($userid == 0) {
        $userid = $USER->id;
    }
    if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
        mtrace("Completion status change for cmid: {$cmid}, userid: {$userid}, newstate: {$newstate}");
    }
    $sql = "SELECT i.id itemid, c.id checkid, c.usertimestamp, i.checklist FROM {checklist_item} i ";
    $sql .= "JOIN {checklist} cl ON i.checklist = cl.id ";
    $sql .= "LEFT JOIN {checklist_check} c ON (c.item = i.id AND c.userid = ?) ";
    $sql .= "WHERE cl.autoupdate > 0 AND i.moduleid = ? AND i.itemoptional < 2 ";
    $items = $DB->get_records_sql($sql, array($userid, $cmid));
    // itemoptional - 0: required; 1: optional; 2: heading;
    // not loading defines from mod/checklist/locallib.php to reduce overhead
    if (empty($items)) {
        if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
            mtrace("No checklist items linked to this course module");
        }
        return 0;
    }
    $newstate = $newstate == COMPLETION_COMPLETE || $newstate == COMPLETION_COMPLETE_PASS;
    // Not complete if failed
    $updatecount = 0;
    $updatechecklists = array();
    foreach ($items as $item) {
        if ($item->checkid) {
            if ($newstate) {
                if ($item->usertimestamp) {
                    continue;
                }
                $check = new stdClass();
                $check->id = $item->checkid;
                $check->usertimestamp = time();
                $DB->update_record('checklist_check', $check);
                $updatechecklists[] = $item->checklist;
                $updatecount++;
            } else {
                if (!$item->usertimestamp) {
                    continue;
                }
                $check = new stdClass();
                $check->id = $item->checkid;
                $check->usertimestamp = 0;
                $DB->update_record('checklist_check', $check);
                $updatechecklists[] = $item->checklist;
                $updatecount++;
            }
        } else {
            if (!$newstate) {
                continue;
            }
            $check = new stdClass();
            $check->item = $item->itemid;
            $check->userid = $userid;
            $check->usertimestamp = time();
            $check->teachertimestamp = 0;
            $check->teachermark = 0;
            // CHECKLIST_TEACHERMARK_UNDECIDED - not loading from mod/checklist/lib.php to reduce overhead
            $check->id = $DB->insert_record('checklist_check', $check);
            $updatechecklists[] = $item->checklist;
            $updatecount++;
        }
    }
    if (!empty($updatechecklists)) {
        $updatechecklists = array_unique($updatechecklists);
        list($csql, $cparams) = $DB->get_in_or_equal($updatechecklists);
        $checklists = $DB->get_records_select('checklist', 'id ' . $csql, $cparams);
        require_once $CFG->dirroot . '/mod/checklist/lib.php';
        foreach ($checklists as $checklist) {
            checklist_update_grades($checklist, $userid);
        }
    }
    if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
        mtrace("Updated {$updatecount} checklist items from this completion status change");
    }
    return $updatecount;
}
function checklist_update_all_grades()
{
    $checklists = get_records('checklist');
    if ($checklists) {
        foreach ($checklists as $checklist) {
            checklist_update_grades($checklist);
        }
    }
}
Beispiel #4
0
 function update_all_autoupdate_checks()
 {
     global $DB;
     if (!$this->checklist->autoupdate) {
         return;
     }
     $users = get_users_by_capability($this->context, 'mod/checklist:updateown', 'u.id', '', '', '', '', '', false);
     if (!$users) {
         return;
     }
     $userids = implode(',', array_keys($users));
     // Get a list of all the checklist items with a module linked to them (ignoring headings)
     $sql = "SELECT cm.id AS cmid, m.name AS mod_name, i.id AS itemid, cm.completion AS completion\n        FROM {modules} m, {course_modules} cm, {checklist_item} i\n        WHERE m.id = cm.module AND cm.id = i.moduleid AND i.moduleid > 0 AND i.checklist = ? AND i.itemoptional != 2";
     $completion = new completion_info($this->course);
     $using_completion = $completion->is_enabled();
     $items = $DB->get_records_sql($sql, array($this->checklist->id));
     foreach ($items as $item) {
         if ($using_completion && $item->completion) {
             $fakecm = new stdClass();
             $fakecm->id = $item->cmid;
             foreach ($users as $user) {
                 $comp_data = $completion->get_data($fakecm, false, $user->id);
                 if ($comp_data->completionstate == COMPLETION_COMPLETE || $comp_data->completionstate == COMPLETION_COMPLETE_PASS) {
                     $check = $DB->get_record('checklist_check', array('item' => $item->itemid, 'userid' => $user->id));
                     if ($check) {
                         if ($check->usertimestamp) {
                             continue;
                         }
                         $check->usertimestamp = time();
                         $DB->update_record('checklist_check', $check);
                     } else {
                         $check = new stdClass();
                         $check->item = $item->itemid;
                         $check->userid = $user->id;
                         $check->usertimestamp = time();
                         $check->teachertimestamp = 0;
                         $check->teachermark = CHECKLIST_TEACHERMARK_UNDECIDED;
                         $check->id = $DB->insert_record('checklist_check', $check);
                     }
                 }
             }
             continue;
         }
         $logaction = '';
         $logaction2 = false;
         switch ($item->mod_name) {
             case 'survey':
                 $logaction = 'submit';
                 break;
             case 'quiz':
                 $logaction = 'close attempt';
                 break;
             case 'forum':
                 $logaction = 'add post';
                 $logaction2 = 'add discussion';
                 break;
             case 'resource':
                 $logaction = 'view';
                 break;
             case 'hotpot':
                 $logaction = 'submit';
                 break;
             case 'wiki':
                 $logaction = 'edit';
                 break;
             case 'checklist':
                 $logaction = 'complete';
                 break;
             case 'choice':
                 $logaction = 'choose';
                 break;
             case 'lams':
                 $logaction = 'view';
                 break;
             case 'scorm':
                 $logaction = 'view';
                 break;
             case 'assignment':
                 $logaction = 'upload';
                 break;
             case 'journal':
                 $logaction = 'add entry';
                 break;
             case 'lesson':
                 $logaction = 'end';
                 break;
             case 'realtimequiz':
                 $logaction = 'submit';
                 break;
             case 'workshop':
                 $logaction = 'submit';
                 break;
             case 'glossary':
                 $logaction = 'add entry';
                 break;
             case 'data':
                 $logaction = 'add';
                 break;
             case 'chat':
                 $logaction = 'talk';
                 break;
             case 'feedback':
                 $logaction = 'submit';
                 break;
             default:
                 continue 2;
                 break;
         }
         $sql = 'SELECT DISTINCT userid ';
         $sql .= "FROM {log} ";
         $sql .= "WHERE cmid = ? AND (action = ?";
         if ($logaction2) {
             $sql .= ' OR action = ?';
         }
         $sql .= ") AND userid IN ({$userids})";
         $log_entries = $DB->get_records_sql($sql, array($item->cmid, $logaction, $logaction2));
         if (!$log_entries) {
             continue;
         }
         foreach ($log_entries as $entry) {
             //echo "User: {$entry->userid} has completed '{$item->mod_name}' with cmid {$item->cmid}, so updating checklist item {$item->itemid}<br />\n";
             $check = $DB->get_record('checklist_check', array('item' => $item->itemid, 'userid' => $entry->userid));
             if ($check) {
                 if ($check->usertimestamp) {
                     continue;
                 }
                 $check->usertimestamp = time();
                 $DB->update_record('checklist_check', $check);
             } else {
                 $check = new stdClass();
                 $check->item = $item->itemid;
                 $check->userid = $entry->userid;
                 $check->usertimestamp = time();
                 $check->teachertimestamp = 0;
                 $check->teachermark = CHECKLIST_TEACHERMARK_UNDECIDED;
                 $check->id = $DB->insert_record('checklist_check', $check);
             }
         }
         // Always update the grades
         checklist_update_grades($this->checklist);
     }
 }
Beispiel #5
0
 public function update_all_autoupdate_checks()
 {
     global $DB, $CFG;
     if (!$this->checklist->autoupdate) {
         return;
     }
     $users = get_users_by_capability($this->context, 'mod/checklist:updateown', 'u.id', '', '', '', '', '', false);
     if (!$users) {
         return;
     }
     $userids = array_keys($users);
     if ($CFG->branch < 26) {
         // No auto class loading before Moodle 2.6.
         require_once $CFG->dirroot . '/mod/checklist/classes/local/autoupdate.php';
     }
     // Get a list of all the checklist items with a module linked to them (ignoring headings).
     $sql = "SELECT cm.id AS cmid, m.name AS mod_name, i.id AS itemid, cm.completion AS completion\n        FROM {modules} m, {course_modules} cm, {checklist_item} i\n        WHERE m.id = cm.module AND cm.id = i.moduleid AND i.moduleid > 0 AND i.checklist = ? AND i.itemoptional != 2";
     $completion = new completion_info($this->course);
     $usingcompletion = $completion->is_enabled();
     $items = $DB->get_records_sql($sql, array($this->checklist->id));
     foreach ($items as $item) {
         if ($usingcompletion && $item->completion) {
             $fakecm = new stdClass();
             $fakecm->id = $item->cmid;
             foreach ($users as $user) {
                 $compdata = $completion->get_data($fakecm, false, $user->id);
                 if ($compdata->completionstate == COMPLETION_COMPLETE || $compdata->completionstate == COMPLETION_COMPLETE_PASS) {
                     $check = $DB->get_record('checklist_check', array('item' => $item->itemid, 'userid' => $user->id));
                     if ($check) {
                         if ($check->usertimestamp) {
                             continue;
                         }
                         $check->usertimestamp = time();
                         $DB->update_record('checklist_check', $check);
                     } else {
                         $check = new stdClass();
                         $check->item = $item->itemid;
                         $check->userid = $user->id;
                         $check->usertimestamp = time();
                         $check->teachertimestamp = 0;
                         $check->teachermark = CHECKLIST_TEACHERMARK_UNDECIDED;
                         $check->id = $DB->insert_record('checklist_check', $check);
                     }
                 }
             }
             continue;
         }
         $loguserids = mod_checklist\local\autoupdate::get_logged_userids($item->mod_name, $item->cmid, $userids);
         if (!$loguserids) {
             continue;
         }
         foreach ($loguserids as $loguserid) {
             $check = $DB->get_record('checklist_check', array('item' => $item->itemid, 'userid' => $loguserid));
             if ($check) {
                 if ($check->usertimestamp) {
                     continue;
                 }
                 $check->usertimestamp = time();
                 $DB->update_record('checklist_check', $check);
             } else {
                 $check = new stdClass();
                 $check->item = $item->itemid;
                 $check->userid = $loguserid;
                 $check->usertimestamp = time();
                 $check->teachertimestamp = 0;
                 $check->teachermark = CHECKLIST_TEACHERMARK_UNDECIDED;
                 $check->id = $DB->insert_record('checklist_check', $check);
             }
         }
         // Always update the grades.
         checklist_update_grades($this->checklist);
     }
 }
 function updateteachermarks()
 {
     global $USER;
     $newchecks = optional_param('items', array(), PARAM_TEXT);
     if (!is_array($newchecks)) {
         // Something has gone wrong, so update nothing
         return;
     }
     if ($this->checklist->teacheredit != CHECKLIST_MARKING_STUDENT) {
         foreach ($newchecks as $newcheck) {
             list($itemid, $newval) = explode(':', $newcheck, 2);
             if (isset($this->items[$itemid])) {
                 $item = $this->items[$itemid];
                 if ($newval != $item->teachermark) {
                     $item->teachermark = $newval;
                     $newcheck = new stdClass();
                     $newcheck->teachertimestamp = time();
                     $newcheck->teachermark = $newval;
                     $oldcheck = get_record_select('checklist_check', 'item = ' . $item->id . ' AND userid = ' . $this->userid);
                     if ($oldcheck) {
                         $newcheck->id = $oldcheck->id;
                         update_record('checklist_check', $newcheck);
                     } else {
                         $newcheck->item = $itemid;
                         $newcheck->userid = $this->userid;
                         $newcheck->id = insert_record('checklist_check', $newcheck);
                     }
                     checklist_update_grades($this->checklist, $this->userid);
                 }
             }
         }
     }
     $newcomments = optional_param('teachercomment', false, PARAM_TEXT);
     if (!$this->checklist->teachercomments || !$newcomments || !is_array($newcomments)) {
         return;
     }
     $itemids = implode(',', array_keys($this->items));
     $commentsunsorted = get_records_select('checklist_comment', "userid = {$this->userid} AND itemid IN ({$itemids})");
     $comments = array();
     if ($commentsunsorted) {
         foreach ($commentsunsorted as $comment) {
             $comments[$comment->itemid] = $comment;
         }
     }
     foreach ($newcomments as $itemid => $newcomment) {
         $newcomment = trim($newcomment);
         if ($newcomment == '') {
             if (array_key_exists($itemid, $comments)) {
                 delete_records('checklist_comment', 'id', $comments[$itemid]->id);
                 unset($comments[$itemid]);
                 // Should never be needed, but just in case...
             }
         } else {
             if (array_key_exists($itemid, $comments)) {
                 if ($comments[$itemid]->text != $newcomment) {
                     $updatecomment = new stdClass();
                     $updatecomment->id = $comments[$itemid]->id;
                     $updatecomment->userid = $this->userid;
                     $updatecomment->itemid = $itemid;
                     $updatecomment->commentby = $USER->id;
                     $updatecomment->text = $newcomment;
                     update_record('checklist_comment', $updatecomment);
                 }
             } else {
                 $addcomment = new stdClass();
                 $addcomment->itemid = $itemid;
                 $addcomment->userid = $this->userid;
                 $addcomment->commentby = $USER->id;
                 $addcomment->text = $newcomment;
                 insert_record('checklist_comment', $addcomment);
             }
         }
     }
 }