/*if ($context->contextlevel == CONTEXT_SYSTEM or $context->contextlevel == CONTEXT_COURSECAT) { require_once $CFG->libdir.'/adminlib.php'; require_login(); admin_externalpage_setup('letters'); $admin = true; } else if ($context->contextlevel == CONTEXT_COURSE) { require_login($context->instanceid); $admin = false; } else { error('Incorrect context level'); } */ $letters = grade_get_letters($context); if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC')) { $old_ids = array_keys($records); // print_r($records); } $action_label = ""; $action_update_count = 0; $action_insert_count = 0; foreach ($letters as $boundary => $letter) { $letter = trim($letter); if ($letter == '') { continue; } $ori_letter = $letter; $letter = str_replace("+", "1", $letter); $newboundary = $_GET['textbox_' . $letter];
/** * Fetches and returns an array of grade letters indexed by their grade boundaries, as stored in course item settings or site preferences. * @return array */ function get_grade_letters() { global $COURSE; $context = get_context_instance(CONTEXT_COURSE, $COURSE->id); return grade_get_letters($context); }
/** * Returns grade letters array used in context * @param object $context object or null for defaults * @return array of grade_boundary=>letter_string */ function grade_get_letters($context = null) { if (empty($context)) { //default grading letters return array('93' => 'A', '90' => 'A-', '87' => 'B+', '83' => 'B', '80' => 'B-', '77' => 'C+', '73' => 'C', '70' => 'C-', '67' => 'D+', '60' => 'D', '0' => 'F'); } static $cache = array(); if (array_key_exists($context->id, $cache)) { return $cache[$context->id]; } if (count($cache) > 100) { $cache = array(); // cache size limit } $letters = array(); $contexts = get_parent_contexts($context); array_unshift($contexts, $context->id); foreach ($contexts as $ctxid) { if ($records = get_records('grade_letters', 'contextid', $ctxid, 'lowerboundary DESC')) { foreach ($records as $record) { $letters[$record->lowerboundary] = $record->letter; } } if (!empty($letters)) { $cache[$context->id] = $letters; return $letters; } } $letters = grade_get_letters(null); $cache[$context->id] = $letters; return $letters; }
/** * Processes the data sent by the form (grades and feedbacks). * Caller is reposible for all access control checks * @param array $data form submission (with magic quotes) * @return array empty array if success, array of warnings if something fails. */ function process_data($data) { $warnings = array(); $separategroups = false; $mygroups = array(); if ($this->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $this->context)) { $separategroups = true; $mygroups = groups_get_user_groups($this->course->id); $mygroups = $mygroups[0]; // ignore groupings // reorder the groups fro better perf bellow $current = array_search($this->currentgroup, $mygroups); if ($current !== false) { unset($mygroups[$current]); array_unshift($mygroups, $this->currentgroup); } } // always initialize all arrays $queue = array(); foreach ($data as $varname => $postedvalue) { $oldvalue = $data->{'old' . $varname}; // was change requested? /// BP: moved this up to speed up the process of eliminating unchanged values if ($oldvalue == $postedvalue) { // string comparison continue; } $needsupdate = false; // skip, not a grade nor feedback if (strpos($varname, 'grade') === 0) { $data_type = 'grade'; } else { if (strpos($varname, 'feedback') === 0) { $data_type = 'feedback'; } else { continue; } } $gradeinfo = explode("_", $varname); $userid = clean_param($gradeinfo[1], PARAM_INT); $itemid = clean_param($gradeinfo[2], PARAM_INT); // HACK: bob puffer call local object if (!($grade_item = grade_item::fetch(array('id' => $itemid, 'courseid' => $this->courseid)))) { // we must verify course id here! // if (!$grade_item = grade_item_local::fetch(array('id'=>$itemid, 'courseid'=>$this->courseid))) { // we must verify course id here! // END OF HACK error('Incorrect grade item id'); } // Pre-process grade if ($data_type == 'grade') { $feedback = false; $feedbackformat = false; if ($grade_item->gradetype == GRADE_TYPE_SCALE) { if ($postedvalue == -1) { // -1 means no grade $finalgrade = null; } else { $finalgrade = $postedvalue; } } else { // HACK: bob puffer to allow calculating grades from input letters $context = get_context_instance(CONTEXT_COURSE, $grade_item->courseid); // percentage input if (strpos($postedvalue, '%')) { $percent = trim(substr($postedvalue, 0, strpos($postedvalue, '%'))); $postedvalue = $percent * 0.01 * $grade_item->grademax; // letter input? } elseif ($letters = grade_get_letters($context)) { unset($lastitem); foreach ($letters as $used => $letter) { if (strtoupper($postedvalue) == $letter) { if (isset($lastitem)) { $postedvalue = $lastitem; } else { $postedvalue = $grade_item->grademax; } break; } else { $lastitem = ($used - 1) * 0.01 * $grade_item->grademax; } } } // END OF HACK $finalgrade = unformat_float($postedvalue); } $errorstr = ''; // Warn if the grade is out of bounds. if (is_null($finalgrade)) { // ok } else { $bounded = $grade_item->bounded_grade($finalgrade); if ($bounded > $finalgrade) { $errorstr = 'lessthanmin'; } else { if ($bounded < $finalgrade) { $errorstr = 'morethanmax'; } } } if ($errorstr) { $user = get_record('user', 'id', $userid, '', '', '', '', 'id, firstname, lastname'); $gradestr = new object(); $gradestr->username = fullname($user); $gradestr->itemname = $grade_item->get_name(); $warnings[] = get_string($errorstr, 'grades', $gradestr); } } else { if ($data_type == 'feedback') { $finalgrade = false; $trimmed = trim($postedvalue); if (empty($trimmed)) { $feedback = NULL; } else { $feedback = stripslashes($postedvalue); } } } // group access control if ($separategroups) { // note: we can not use $this->currentgroup because it would fail badly // when having two browser windows each with different group $sharinggroup = false; foreach ($mygroups as $groupid) { if (groups_is_member($groupid, $userid)) { $sharinggroup = true; break; } } if (!$sharinggroup) { // either group membership changed or somebedy is hacking grades of other group $warnings[] = get_string('errorsavegrade', 'grades'); continue; } } $grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE); } return $warnings; }