$items = optional_param_array('items', false, PARAM_INT); } $url = new moodle_url('/mod/checklist/view.php', array('id' => $id)); $cm = get_coursemodule_from_id('checklist', $id, 0, false, MUST_EXIST); $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); $checklist = $DB->get_record('checklist', array('id' => $cm->instance), '*', MUST_EXIST); $PAGE->set_url($url); require_login($course, true, $cm); if ($CFG->version < 2011120100) { $context = get_context_instance(CONTEXT_MODULE, $cm->id); } else { $context = context_module::instance($cm->id); } $userid = $USER->id; if (!has_capability('mod/checklist:updateown', $context)) { echo 'Error: you do not have permission to update this checklist'; die; } if (!confirm_sesskey()) { echo 'Error: invalid sesskey'; die; } if (!$items || !is_array($items)) { echo 'Error: invalid (or missing) items list'; die; } if (!empty($items)) { $chk = new checklist_class($cm->id, $userid, $checklist, $cm, $course); $chk->ajaxupdatechecks($items); } echo 'OK';
/** * Check off the given items in the checklist for the given user * * @Given /^the following items are checked off in checklist "(?P<checklist_string>[^"]*)" for user "(?P<user_string>[^"]*)":$/ * @param string $checklistname * @param $username * @param TableNode $table */ public function the_following_items_are_checked_off_in_checklist_for_user($checklistname, $username, TableNode $table) { global $DB, $CFG; require_once $CFG->dirroot . '/mod/checklist/locallib.php'; $required = array('itemtext'); $optional = array('studentmark' => 'yes', 'teachermark' => 'none', 'teachername' => 'admin'); // Valid settings for field 'studentmark'. $studentmarkmap = array('yes' => 1, 'no' => 0); // Valid settings for field 'teachermark'. $teachermarkmap = array('none' => CHECKLIST_TEACHERMARK_UNDECIDED, 'yes' => CHECKLIST_TEACHERMARK_YES, 'no' => CHECKLIST_TEACHERMARK_NO); $data = $table->getHash(); $firstrow = reset($data); // Check required fields are present. foreach ($required as $reqname) { if (!isset($firstrow[$reqname])) { throw new Exception('Checklist item updates require the field ' . $reqname . ' to be set'); } } // Get the checklist data. $checklist = $DB->get_record('checklist', array('name' => $checklistname), '*', MUST_EXIST); list($course, $cm) = get_course_and_cm_from_instance($checklist, 'checklist'); $userid = $DB->get_field('user', 'id', array('username' => $username), MUST_EXIST); $chk = new checklist_class($cm->id, $userid, $checklist, $cm, $course); $updatestudent = $checklist->teacheredit != CHECKLIST_MARKING_TEACHER && isset($firstrow['studentmark']); $updateteacher = $checklist->teacheredit != CHECKLIST_MARKING_STUDENT && isset($firstrow['teachermark']); if (!$updateteacher && !$updatestudent) { throw new Exception('Checklist update must specify a teachermark (for teacher/both checklists) or a studentmark ' . '(for student/both checklists)'); } // Gather together all the updated marks. $studentupdates = array(); $teacherupdates = array(); foreach ($data as $row) { $update = $optional; foreach ($row as $fieldname => $value) { if (!in_array($fieldname, $required) && !isset($optional[$fieldname])) { throw new Exception('Unknown checklist item update field \'', $fieldname . '\''); } $update[$fieldname] = $value; } if (!in_array($update['studentmark'], $studentmarkmap)) { throw new Exception('Invalid studentmark value \'' . $update['studentmark'] . '\' in checklist update'); } if (!in_array($update['teachermark'], $teachermarkmap)) { throw new Exception('Invalid teachermark value \'' . $update['teachermark'] . '\' in checklist update'); } $itemid = $chk->get_itemid_by_name($update['itemtext']); if ($updatestudent) { $studentupdates[$itemid] = $studentmarkmap[$update['studentmark']]; } if ($updateteacher) { if (!isset($teacherupdates[$update['teachername']])) { $teacherupdates[$update['teachername']] = array(); } $teacherupdates[$update['teachername']][$itemid] = $teachermarkmap[$update['teachermark']]; } } // Process the updated marks. if ($updatestudent) { $chk->ajaxupdatechecks($studentupdates); } if ($updateteacher) { foreach ($teacherupdates as $teachername => $checkmarks) { $teacherid = $DB->get_field('user', 'id', array('username' => $teachername), MUST_EXIST); $chk->update_teachermarks($checkmarks, $teacherid); } } }