/** * Perform minimal validation on the grade form * @param array $data * @param array $files */ function validation($data, $files) { global $DB; $errors = parent::validation($data, $files); // advanced grading if (!array_key_exists('grade', $data)) { return $errors; } if ($this->assignment->get_instance()->grade > 0) { if (unformat_float($data['grade']) === null && (!empty($data['grade']))) { $errors['grade'] = get_string('invalidfloatforgrade', 'assign', $data['grade']); } else if (unformat_float($data['grade']) > $this->assignment->get_instance()->grade) { $errors['grade'] = get_string('gradeabovemaximum', 'assign', $this->assignment->get_instance()->grade); } else if (unformat_float($data['grade']) < 0) { $errors['grade'] = get_string('gradebelowzero', 'assign'); } } else { // this is a scale if ($scale = $DB->get_record('scale', array('id'=>-($this->assignment->get_instance()->grade)))) { $scaleoptions = make_menu_from_list($scale->scale); if (!array_key_exists((int)$data['grade'], $scaleoptions)) { $errors['grade'] = get_string('invalidgradeforscale', 'assign'); } } } return $errors; }
protected function get_scale_selection() { global $DB; $vplinstance = $this->vpl->get_instance(); $scaleid = $this->vpl->get_grade(); $options = array(); $options[-1] = get_string('nograde'); if ($scaleid > 0) { for ($i = 0; $i <= $scaleid; $i++) { $options[$i] = $i . ' / ' . $scaleid; } } elseif ($scaleid < 0) { $scaleid = -$scaleid; if ($scale = $DB->get_record('scale', array('id' => $scaleid))) { $options = $options + make_menu_from_list($scale->scale); } } return $options; }
function journal_grades($journalid) { /// Must return an array of grades, indexed by user, and a max grade. if (!($journal = get_record("journal", "id", $journalid))) { return NULL; } $grades = get_records_menu("journal_entries", "journal", $journal->id, "", "userid,rating"); if ($journal->assessed > 0) { $return->grades = $grades; $return->maxgrade = $journal->assessed; } else { if ($journal->assessed == 0) { return NULL; } else { if ($scale = get_record("scale", "id", -$journal->assessed)) { $scalegrades = make_menu_from_list($scale->scale); if ($grades) { foreach ($grades as $key => $grade) { $grades[$key] = $scalegrades[$grade]; } } } $return->grades = $grades; $return->maxgrade = ""; } } return $return; }
/** * Create this grade import form */ public function definition() { global $CFG, $PAGE, $DB; $mform = $this->_form; $params = $this->_customdata; $renderer = $PAGE->get_renderer('assign'); // Visible elements. $assignment = $params['assignment']; $csvdata = $params['csvdata']; $gradeimporter = $params['gradeimporter']; $update = false; $ignoremodified = $params['ignoremodified']; $draftid = $params['draftid']; if (!$gradeimporter) { print_error('invalidarguments'); return; } if ($csvdata) { $gradeimporter->parsecsv($csvdata); } $scaleoptions = null; if ($assignment->get_instance()->grade < 0) { if ($scale = $DB->get_record('scale', array('id' => -$assignment->get_instance()->grade))) { $scaleoptions = make_menu_from_list($scale->scale); } } if (!$gradeimporter->init()) { $thisurl = new moodle_url('/mod/assign/view.php', array('action' => 'viewpluginpage', 'pluginsubtype' => 'assignfeedback', 'plugin' => 'offline', 'pluginaction' => 'uploadgrades', 'id' => $assignment->get_course_module()->id)); print_error('invalidgradeimport', 'assignfeedback_offline', $thisurl); return; } $mform->addElement('header', 'importgrades', get_string('importgrades', 'assignfeedback_offline')); $updates = array(); while ($record = $gradeimporter->next()) { $user = $record->user; $grade = $record->grade; $modified = $record->modified; $userdesc = fullname($user); if ($assignment->is_blind_marking()) { $userdesc = get_string('hiddenuser', 'assign') . $assignment->get_uniqueid_for_user($user->id); } $usergrade = $assignment->get_user_grade($user->id, false); // Note: we lose the seconds when converting to user date format - so must not count seconds in comparision. $skip = false; $stalemodificationdate = $usergrade && $usergrade->timemodified > $modified + 60; if (!empty($scaleoptions)) { // This is a scale - we need to convert any grades to indexes in the scale. $scaleindex = array_search($grade, $scaleoptions); if ($scaleindex !== false) { $grade = $scaleindex; } else { $grade = ''; } } else { $grade = unformat_float($grade); } if ($usergrade && $usergrade->grade == $grade) { // Skip - grade not modified. $skip = true; } else { if (!isset($grade) || $grade === '' || $grade < 0) { // Skip - grade has no value. $skip = true; } else { if (!$ignoremodified && $stalemodificationdate) { // Skip - grade has been modified. $skip = true; } else { if ($assignment->grading_disabled($user->id)) { // Skip grade is locked. $skip = true; } else { if ($assignment->get_instance()->grade > -1 && ($grade < 0 || $grade > $assignment->get_instance()->grade)) { // Out of range. $skip = true; } } } } } if (!$skip) { $update = true; if (!empty($scaleoptions)) { $formattedgrade = $scaleoptions[$grade]; } else { $formattedgrade = format_float($grade, 2); } $updates[] = get_string('gradeupdate', 'assignfeedback_offline', array('grade' => $formattedgrade, 'student' => $userdesc)); } if ($ignoremodified || !$stalemodificationdate) { foreach ($record->feedback as $feedback) { $plugin = $feedback['plugin']; $field = $feedback['field']; $newvalue = $feedback['value']; $description = $feedback['description']; $oldvalue = ''; if ($usergrade) { $oldvalue = $plugin->get_editor_text($field, $usergrade->id); } if ($newvalue != $oldvalue) { $update = true; $updates[] = get_string('feedbackupdate', 'assignfeedback_offline', array('text' => $newvalue, 'field' => $description, 'student' => $userdesc)); } } } } $gradeimporter->close(false); if ($update) { $mform->addElement('html', $renderer->list_block_contents(array(), $updates)); } else { $mform->addElement('html', get_string('nochanges', 'assignfeedback_offline')); } $mform->addElement('hidden', 'id', $assignment->get_course_module()->id); $mform->setType('id', PARAM_INT); $mform->addElement('hidden', 'action', 'viewpluginpage'); $mform->setType('action', PARAM_ALPHA); $mform->addElement('hidden', 'confirm', 'true'); $mform->setType('confirm', PARAM_BOOL); $mform->addElement('hidden', 'plugin', 'offline'); $mform->setType('plugin', PARAM_PLUGIN); $mform->addElement('hidden', 'pluginsubtype', 'assignfeedback'); $mform->setType('pluginsubtype', PARAM_PLUGIN); $mform->addElement('hidden', 'pluginaction', 'uploadgrades'); $mform->setType('pluginaction', PARAM_ALPHA); $mform->addElement('hidden', 'importid', $gradeimporter->importid); $mform->setType('importid', PARAM_INT); $mform->addElement('hidden', 'encoding', $gradeimporter->get_encoding()); $mform->setType('encoding', PARAM_ALPHAEXT); $mform->addElement('hidden', 'separator', $gradeimporter->get_separator()); $mform->setType('separator', PARAM_ALPHA); $mform->addElement('hidden', 'ignoremodified', $ignoremodified); $mform->setType('ignoremodified', PARAM_BOOL); $mform->addElement('hidden', 'draftid', $draftid); $mform->setType('draftid', PARAM_INT); if ($update) { $this->add_action_buttons(true, get_string('confirm')); } else { $mform->addElement('cancel'); $mform->closeHeaderBefore('cancel'); } }
} else { if ($subaction == 'grades') { $sql = "\n SELECT \n a.id,\n a.studentid,\n a.grade,\n a.appointmentnote,\n u.lastname,\n u.firstname\n FROM \n {$CFG->prefix}user AS u,\n {$CFG->prefix}scheduler_slots AS s,\n {$CFG->prefix}scheduler_appointment AS a\n WHERE\n u.id = a.studentid AND\n a.slotid = s.id AND\n s.schedulerid = {$scheduler->id} AND\n a.attended = 1\n ORDER BY\n u.lastname,u.firstname,s.teacherid\n "; $grades = get_records_sql($sql); foreach ($grades as $grade) { if ($scheduler->scale > 0) { // numeric scales $finals[$grade->studentid]->sum = @$finals[$grade->studentid]->sum + $grade->grade; $finals[$grade->studentid]->count = @$finals[$grade->studentid]->count + 1; $finals[$grade->studentid]->max = @$finals[$grade->studentid]->max < $grade->grade ? $grade->grade : @$finals[$studentid]->max; } else { if ($scheduler->scale < 0) { // non numeric scales $scaleid = -$scheduler->scale; if ($scale = get_record('scale', 'id', $scaleid)) { $scalegrades = make_menu_from_list($scale->scale); foreach ($grades as $aGrade) { $finals[$aGrade->studentid]->sum = @$finals[$aGrade->studentid]->sum + $scalegrades[$aGgrade->grade]; $finals[$aGrade->studentid]->count = @$finals[$aGrade->studentid]->count + 1; $finals[$aGrade->studentid]->max = @$finals[$aGrade->studentid]->max < $aGrade ? $scalegrades[$aGgrade->grade] : @$finals[$aGrade->studentid]->max; } } } } $finals[$grade->studentid]->lastname = $grade->lastname; $finals[$grade->studentid]->firstname = $grade->firstname; $finals[$grade->studentid]->appointmentnote = @$finals[$grade->studentid]->appointmentnote . ' | ' . $grade->appointmentnote; } /// Making title line $stream .= get_string('student', 'scheduler') . $csvfieldseparator; $stream .= get_string('grades') . $csvfieldseparator;
echo $OUTPUT->heading($scale->name); echo "<center>"; echo $OUTPUT->select(html_select::make($scalemenu)); echo "</center>"; echo text_to_html($scale->description); echo $OUTPUT->box_end(); echo "<hr />"; } } else { if (has_capability('moodle/course:managescales', $context)) { echo "<p align=\"center\">("; print_string("scalestip"); echo ")</p>"; } } if ($scales = $DB->get_records("scale", array("courseid" => 0), "name ASC")) { echo $OUTPUT->heading($strstandardscales); foreach ($scales as $scale) { $scalemenu = make_menu_from_list($scale->scale); echo $OUTPUT->box_start(); echo $OUTPUT->heading($scale->name); echo "<center>"; echo $OUTPUT->select(html_select::make($scalemenu, '')); echo "</center>"; echo text_to_html($scale->description); echo $OUTPUT->box_end(); echo "<hr />"; } } echo $OUTPUT->close_window_button(); echo $OUTPUT->footer();
/** * Return a grade in user-friendly form, whether it's a scale or not * * @global object * @param mixed $grade * @return string User-friendly representation of grade */ function display_grade($grade) { global $DB; static $scalegrades = array(); // Cache scales for each assignment - they might have different scales!! if ($this->assignment->grade >= 0) { // Normal number if ($grade == -1) { return '-'; } else { return $grade.' / '.$this->assignment->grade; } } else { // Scale if (empty($scalegrades[$this->assignment->id])) { if ($scale = $DB->get_record('scale', array('id'=>-($this->assignment->grade)))) { $scalegrades[$this->assignment->id] = make_menu_from_list($scale->scale); } else { return '-'; } } if (isset($scalegrades[$this->assignment->id][$grade])) { return $scalegrades[$this->assignment->id][$grade]; } return '-'; } }
/** * A small utility function for making scale menus * */ function make_grading_menu(&$brainstorm, $id, $selected = '', $return = false) { if (!$brainstorm->scale) { return ''; } if ($brainstorm->scale > 0) { for ($i = 0; $i <= $brainstorm->scale; $i++) { $scalegrades[$i] = $i; } } else { $scaleid = -$brainstorm->scale; if ($scale = get_record('scale', 'id', $scaleid)) { $scalegrades = make_menu_from_list($scale->scale); } } return choose_from_menu($scalegrades, $id, $selected, 'choose', '', '', $return); }
/** * Loop through uploaded grades and update the grades for this assignment * * @param int $draftid - The unique draft item id for this import * @param int $importid - The unique import ID for this csv import operation * @param bool $ignoremodified - Ignore the last modified date when checking fields * @return string - The html response */ public function process_import_grades($draftid, $importid, $ignoremodified) { global $USER, $DB; require_sesskey(); require_capability('mod/assign:grade', $this->assignment->get_context()); $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment); $context = context_user::instance($USER->id); $fs = get_file_storage(); if (!($files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false))) { redirect(new moodle_url('view.php', array('id' => $this->assignment->get_course_module()->id, 'action' => 'grading'))); return; } $file = reset($files); $csvdata = $file->get_content(); if ($csvdata) { $gradeimporter->parsecsv($csvdata); } if (!$gradeimporter->init()) { $thisurl = new moodle_url('/mod/assign/view.php', array('action' => 'viewpluginpage', 'pluginsubtype' => 'assignfeedback', 'plugin' => 'offline', 'pluginaction' => 'uploadgrades', 'id' => $assignment->get_course_module()->id)); print_error('invalidgradeimport', 'assignfeedback_offline', $thisurl); return; } // Does this assignment use a scale? $scaleoptions = null; if ($this->assignment->get_instance()->grade < 0) { if ($scale = $DB->get_record('scale', array('id' => -$this->assignment->get_instance()->grade))) { $scaleoptions = make_menu_from_list($scale->scale); } } // We may need to upgrade the gradebook comments after this update. $adminconfig = $this->assignment->get_admin_config(); $gradebookplugin = $adminconfig->feedback_plugin_for_gradebook; $updatecount = 0; while ($record = $gradeimporter->next()) { $user = $record->user; $modified = $record->modified; $userdesc = fullname($user); $usergrade = $this->assignment->get_user_grade($user->id, false); if (!empty($scaleoptions)) { // This is a scale - we need to convert any grades to indexes in the scale. $scaleindex = array_search($record->grade, $scaleoptions); if ($scaleindex !== false) { $record->grade = $scaleindex; } else { $record->grade = ''; } } else { $record->grade = unformat_float($record->grade); } // Note: Do not count the seconds when comparing modified dates. $skip = false; $stalemodificationdate = $usergrade && $usergrade->timemodified > $modified + 60; if ($usergrade && $usergrade->grade == $record->grade) { // Skip - grade not modified. $skip = true; } else { if (!isset($record->grade) || $record->grade === '' || $record->grade < 0) { // Skip - grade has no value. $skip = true; } else { if (!$ignoremodified && $stalemodificationdate) { // Skip - grade has been modified. $skip = true; } else { if ($this->assignment->grading_disabled($record->user->id)) { // Skip grade is locked. $skip = true; } else { if ($this->assignment->get_instance()->grade > -1 && ($record->grade < 0 || $record->grade > $this->assignment->get_instance()->grade)) { // Out of range. $skip = true; } } } } } if (!$skip) { $grade = $this->assignment->get_user_grade($record->user->id, true); $grade->grade = $record->grade; $grade->grader = $USER->id; if ($this->assignment->update_grade($grade)) { $this->assignment->notify_grade_modified($grade); $updatecount += 1; } } if ($ignoremodified || !$stalemodificationdate) { foreach ($record->feedback as $feedback) { $plugin = $feedback['plugin']; $field = $feedback['field']; $newvalue = $feedback['value']; $description = $feedback['description']; $oldvalue = ''; if ($usergrade) { $oldvalue = $plugin->get_editor_text($field, $usergrade->id); if (empty($oldvalue)) { $oldvalue = ''; } } if ($newvalue != $oldvalue) { $updatecount += 1; $grade = $this->assignment->get_user_grade($record->user->id, true); $this->assignment->notify_grade_modified($grade); $plugin->set_editor_text($field, $newvalue, $grade->id); // If this is the gradebook comments plugin - post an update to the gradebook. if ($plugin->get_subtype() . '_' . $plugin->get_type() == $gradebookplugin) { $grade->feedbacktext = $plugin->text_for_gradebook($grade); $grade->feedbackformat = $plugin->format_for_gradebook($grade); $this->assignment->update_grade($grade); } } } } } $gradeimporter->close(true); $renderer = $this->assignment->get_renderer(); $o = ''; $o .= $renderer->render(new assign_header($this->assignment->get_instance(), $this->assignment->get_context(), false, $this->assignment->get_course_module()->id, get_string('importgrades', 'assignfeedback_offline'))); $o .= $renderer->box(get_string('updatedgrades', 'assignfeedback_offline', $updatecount)); $url = new moodle_url('view.php', array('id' => $this->assignment->get_course_module()->id, 'action' => 'grading')); $o .= $renderer->continue_button($url); $o .= $renderer->render_footer(); return $o; }
/** * a utility function for making grading lists * @param reference $scheduler * @param string $id the form field id * @param string $selected the selected value * @param boolean $return if true, prints the list to output elsewhere returns the HTML string. * @return the output of the choose_from_menu production */ function scheduler_make_grading_menu(&$scheduler, $id, $selected = '', $return = false) { if ($scheduler->scale > 0) { for ($i = 0; $i <= $scheduler->scale; $i++) { $scalegrades[$i] = $i; } } else { $scaleid = -$scheduler->scale; if ($scale = get_record('scale', 'id', $scaleid)) { $scalegrades = make_menu_from_list($scale->scale); } } return choose_from_menu($scalegrades, $id, $selected, 'choose', '', '', $return); }
/** * Update a grade in the grade table for the setaskment and in the gradebook. * * @param stdClass $grade a grade record keyed on id * @param bool $reopenattempt If the attempt reopen method is manual, allow another attempt at this setaskment. * @return bool true for success */ public function update_grade($grade, $reopenattempt = false) { global $DB; $grade->timemodified = time(); if (!empty($grade->workflowstate)) { $validstates = $this->get_marking_workflow_states_for_current_user(); if (!array_key_exists($grade->workflowstate, $validstates)) { return false; } } if ($grade->grade && $grade->grade != -1) { if ($this->get_instance()->grade > 0) { if (!is_numeric($grade->grade)) { return false; } else { if ($grade->grade > $this->get_instance()->grade) { return false; } else { if ($grade->grade < 0) { return false; } } } } else { // This is a scale. if ($scale = $DB->get_record('scale', array('id' => -$this->get_instance()->grade))) { $scaleoptions = make_menu_from_list($scale->scale); if (!array_key_exists((int) $grade->grade, $scaleoptions)) { return false; } } } } if (empty($grade->attemptnumber)) { // Set it to the default. $grade->attemptnumber = 0; } $DB->update_record('setask_grades', $grade); $submission = null; if ($this->get_instance()->teamsubmission) { $submission = $this->get_group_submission($grade->userid, 0, false); } else { $submission = $this->get_user_submission($grade->userid, false); } // Only push to gradebook if the update is for the latest attempt. // Not the latest attempt. if ($submission && $submission->attemptnumber != $grade->attemptnumber) { return true; } if ($this->gradebook_item_update(null, $grade)) { \mod_setask\event\submission_graded::create_from_grade($this, $grade)->trigger(); } // If the conditions are met, allow another attempt. if ($submission) { $this->reopen_submission_if_required($grade->userid, $submission, $reopenattempt); } return true; }
/** * Must return an array of grades for a given instance of this module, * indexed by user. It also returns a maximum allowed grade. * @param int $schedulerid the id of the activity module * @return array an array of grades */ function scheduler_grades($cmid) { global $CFG; if (!($module = get_record('course_modules', 'id', $cmid))) { return NULL; } if (!($scheduler = get_record('scheduler', 'id', $module->instance))) { return NULL; } if ($scheduler->scale == 0) { // No grading return NULL; } $query = "\n SELECT\n a.id,\n a.studentid,\n a.grade\n FROM\n {$CFG->prefix}scheduler_slots AS s \n LEFT JOIN\n {$CFG->prefix}scheduler_appointment AS a\n ON\n s.id = a.slotid\n WHERE\n s.schedulerid = {$scheduler->id} AND \n a.grade IS NOT NULL\n "; // echo $query ; $grades = get_records_sql($query); if ($grades) { if ($scheduler->scale > 0) { // Grading numerically $finalgrades = array(); foreach ($grades as $aGrade) { $finals[$aGrade->studentid]->sum = @$finals[$aGrade->studentid]->sum + $aGrade->grade; $finals[$aGrade->studentid]->count = @$finals[$aGrade->studentid]->count + 1; $finals[$aGrade->studentid]->max = @$finals[$aGrade->studentid]->max < $aGrade->grade ? $aGrade->grade : @$finalgrades[$aGrade->studentid]->max; } /// compute the adequate strategy foreach ($finals as $student => $aGradeSet) { switch ($scheduler->gradingstrategy) { case MAX_GRADE: $finalgrades[$student] = $aGradeSet->max; break; case MEAN_GRADE: $finalgrades[$student] = $aGradeSet->sum / $aGradeSet->count; break; } } $return->grades = $finalgrades; $return->maxgrade = $scheduler->scale; } else { // Scales $finalgrades = array(); $scaleid = -$scheduler->scale; $maxgrade = ''; if ($scale = get_record('scale', 'id', $scaleid)) { $scalegrades = make_menu_from_list($scale->scale); foreach ($grades as $aGrade) { $finals[$aGrade->studentid]->sum = @$finals[$aGrade->studentid]->sum + $scalegrades[$aGgrade->grade]; $finals[$aGrade->studentid]->count = @$finals[$aGrade->studentid]->count + 1; $finals[$aGrade->studentid]->max = @$finals[$aGrade->studentid]->max < $aGrade ? $scalegrades[$aGgrade->grade] : @$finals[$aGrade->studentid]->max; } $maxgrade = $scale->name; } /// compute the adequate strategy foreach ($finals as $student => $aGradeSet) { switch ($scheduler->gradingstrategy) { case MAX_GRADE: $finalgrades[$student] = $aGradeSet->max; break; case MEAN_GRADE: $finalgrades[$student] = $aGradeSet->sum / $aGradeSet->count; break; } } $return->grades = $finalgrades; $return->maxgrade = $maxgrade; } return $return; } return NULL; }
/** * Perform minimal validation on the grade form * @param array $data * @param array $files */ public function validation($data, $files) { global $DB; $errors = parent::validation($data, $files); $instance = $this->seplment->get_instance(); if ($instance->markingworkflow && !empty($data['sendstudentnotifications']) && $data['workflowstate'] != ASSIGN_MARKING_WORKFLOW_STATE_RELEASED) { $errors['sendstudentnotifications'] = get_string('studentnotificationworkflowstateerror', 'sepl'); } // Advanced grading. if (!array_key_exists('grade', $data)) { return $errors; } if ($instance->grade > 0) { if (unformat_float($data['grade'], true) === false && !empty($data['grade'])) { $errors['grade'] = get_string('invalidfloatforgrade', 'sepl', $data['grade']); } else { if (unformat_float($data['grade']) > $instance->grade) { $errors['grade'] = get_string('gradeabovemaximum', 'sepl', $instance->grade); } else { if (unformat_float($data['grade']) < 0) { $errors['grade'] = get_string('gradebelowzero', 'sepl'); } } } } else { // This is a scale. if ($scale = $DB->get_record('scale', array('id' => -$instance->grade))) { $scaleoptions = make_menu_from_list($scale->scale); if ((int) $data['grade'] !== -1 && !array_key_exists((int) $data['grade'], $scaleoptions)) { $errors['grade'] = get_string('invalidgradeforscale', 'sepl'); } } } return $errors; }
/** * Must return an array of grades for a given instance of this module, * indexed by user. It also returns a maximum allowed grade. * * Example: * $return->grades = array of grades; * $return->maxgrade = maximum allowed grade; * * return $return; * * @param int $mumiemoduleID of an instance of this module * @return mixed Null or object with an array of grades and with the maximum grade **/ function mumiemodule_grades($mumiemoduleid) { if (!($mumiemodule = get_record('mumiemodule', 'id', $mumiemoduleid))) { return NULL; } if ($mumiemodule->grade == 0) { // No grading return NULL; } $grades = get_records_menu('mumiemodule_students', 'mumiemodule', $mumiemodule > id, '', 'userid, grade'); $return = new object(); if ($mumiemodule->grade > 0) { if ($grades) { foreach ($grades as $userid => $grade) { if ($grade == -1) { $grades[$userid] = '-'; } } } $return->grades = $grades; $return->maxgrade = (int) $mumiemodule->grade; } else { // Scale if ($grades) { $scaleid = -$mumiemodule->grade; $maxgrade = ""; if ($scale = get_record('scale', 'id', $scaleid)) { $scalegrades = make_menu_from_list($scale->scale); foreach ($grades as $userid => $grade) { if (empty($scalegrades[$grade])) { $grades[$userid] = '-'; } else { $grades[$userid] = $scalegrades[$grade]; } } $maxgrade = $scale->name; } } $return->grades = $grades; $return->maxgrade = $maxgrade; } return $return; }
/** * Return grade for given user or all users. * * @param int $schedulerid id of scheduler * @param int $userid optional user id, 0 means all users * @return array array of grades, false if none */ public function get_user_grades($userid = 0) { global $CFG, $DB; if ($this->scale == 0) { return false; } $usersql = ''; $params = array(); if ($userid) { $usersql = ' AND a.studentid = :userid'; $params['userid'] = $userid; } $params['sid'] = $this->id; $sql = 'SELECT a.id, a.studentid, a.grade ' . 'FROM {scheduler_slots} s JOIN {scheduler_appointment} a ON s.id = a.slotid ' . 'WHERE s.schedulerid = :sid AND a.grade IS NOT NULL' . $usersql; $grades = $DB->get_records_sql($sql, $params); $finalgrades = array(); $gradesums = array(); foreach ($grades as $grade) { $gradesums[$grade->studentid] = new stdClass(); $finalgrades[$grade->studentid] = new stdClass(); $finalgrades[$grade->studentid]->userid = $grade->studentid; } if ($this->scale > 0) { // Grading numerically. foreach ($grades as $grade) { $gradesums[$grade->studentid]->sum = @$gradesums[$grade->studentid]->sum + $grade->grade; $gradesums[$grade->studentid]->count = @$gradesums[$grade->studentid]->count + 1; $gradesums[$grade->studentid]->max = @$gradesums[$grade->studentid]->max < $grade->grade ? $grade->grade : @$gradesums[$grade->studentid]->max; } // Retrieve the adequate strategy. foreach ($gradesums as $student => $gradeset) { switch ($this->gradingstrategy) { case SCHEDULER_MAX_GRADE: $finalgrades[$student]->rawgrade = $gradeset->max; break; case SCHEDULER_MEAN_GRADE: $finalgrades[$student]->rawgrade = $gradeset->sum / $gradeset->count; break; } } } else { // Grading on scales. $scaleid = -$this->scale; $maxgrade = ''; if ($scale = $DB->get_record('scale', array('id' => $scaleid))) { $scalegrades = make_menu_from_list($scale->scale); foreach ($grades as $grade) { $gradesums[$grade->studentid]->sum = @$gradesums[$grade->studentid]->sum + $grade->grade; $gradesums[$grade->studentid]->count = @$gradesums[$grade->studentid]->count + 1; $gradesums[$grade->studentid]->max = @$gradesums[$grade->studentid]->max < $grade ? $grade->grade : @$gradesums[$grade->studentid]->max; } $maxgrade = $scale->name; } // Retrieve the adequate strategy. foreach ($gradesums as $student => $gradeset) { switch ($this->gradingstrategy) { case SCHEDULER_MAX_GRADE: $finalgrades[$student]->rawgrade = $gradeset->max; break; case SCHEDULER_MEAN_GRADE: $finalgrades[$student]->rawgrade = $gradeset->sum / $gradeset->count; break; } } } // Include any empty grades. if ($userid > 0) { if (!array_key_exists($userid, $finalgrades)) { $finalgrades[$userid] = new stdClass(); $finalgrades[$userid]->userid = $userid; $finalgrades[$userid]->rawgrade = null; } } else { $gui = new graded_users_iterator($this->get_courserec()); $gui->init(); while ($userdata = $gui->next_user()) { $uid = $userdata->user->id; if (!array_key_exists($uid, $finalgrades)) { $finalgrades[$uid] = new stdClass(); $finalgrades[$uid]->userid = $uid; $finalgrades[$uid]->rawgrade = null; } } } return $finalgrades; }
function elluminate_grades($elluminateid) { global $DB; if (!($elluminate = $DB->get_record('elluminate', array('id' => $elluminateid)))) { return NULL; } if ($elluminate->grade == 0) { // No grading return NULL; } $return = new stdClass(); $grades = $DB->get_records_menu('elluminate_attendance', array('elluminateid' => $elluminateid), '', 'userid,grade'); if ($elluminate->grade > 0) { if ($grades) { foreach ($grades as $userid => $grade) { if ($grade == -1) { $grades[$userid] = '-'; } } } $return->grades = $grades; $return->maxgrade = $elluminate->grade; } else { // Scale if ($grades) { $scaleid = -$elluminate->grade; $maxgrade = ""; if ($scale = $DB->get_record('scale', array('id' => $scaleid))) { $scalegrades = make_menu_from_list($scale->scale); foreach ($grades as $userid => $grade) { if (empty($scalegrades[$grade])) { $grades[$userid] = '-'; } else { $grades[$userid] = $scalegrades[$grade]; } } $maxgrade = $scale->name; } } $return->grades = $grades; $return->maxgrade = $maxgrade; } return $return; }
/** * Update a grade in the grade table for the assignment and in the gradebook * * @param stdClass $grade a grade record keyed on id * @return bool true for success */ public function update_grade($grade) { global $DB; $grade->timemodified = time(); if ($grade->grade && $grade->grade != -1) { if ($this->get_instance()->grade > 0) { if (!is_numeric($grade->grade)) { return false; } else if ($grade->grade > $this->get_instance()->grade) { return false; } else if ($grade->grade < 0) { return false; } } else { // this is a scale if ($scale = $DB->get_record('scale', array('id' => -($this->get_instance()->grade)))) { $scaleoptions = make_menu_from_list($scale->scale); if (!array_key_exists((int) $grade->grade, $scaleoptions)) { return false; } } } } $result = $DB->update_record('assign_grades', $grade); if ($result) { $this->gradebook_item_update(null, $grade); } return $result; }
function organizer_make_grades_menu_organizer($gradingtype) { global $DB; $grades = array(); if ($gradingtype < 0) { if ($scale = $DB->get_record('scale', array('id' => -$gradingtype))) { $menu = make_menu_from_list($scale->scale); $menu['0'] = get_string('nograde'); return $menu; } } else { if ($gradingtype > 0) { $grades['-1'] = get_string('nograde'); for ($i = $gradingtype; $i >= 0; $i--) { $grades[$i] = organizer_clean_num($i) . ' / ' . organizer_clean_num($gradingtype); } return $grades; } } return $grades; }
/** * Return a grade in user-friendly form, whether it's a scale or not * * @param $grade * @return string User-friendly representation of grade */ function display_grade($grade, $mumiemodule) { $scalegrades = array(); if ($mumiemodule->grade >= 0) { // Normal number if ($grade == -1) { return '-'; } else { return $grade . ' / ' . $mumiemodule->grade; } } else { // Scale if (empty($scalegrades[$mumiemodule->id])) { if ($scale = get_record('scale', 'id', -$mumiemodule->grade)) { $scalegrades[$mumiemodule->id] = make_menu_from_list($scale->scale); } else { return '-'; } } if (isset($scalegrades[$mumiemodule->id][$grade])) { return $scalegrades[$mumiemodule->id][$grade]; } return '-'; } }
/** * Must return an array of grades for a given instance of this module, * indexed by user. It also returns a maximum allowed grade. * * Example: * $return->grades = array of grades; * $return->maxgrade = maximum allowed grade; * * return $return; * * @param int $webquestscormid ID of an instance of this module * @return mixed Null or object with an array of grades and with the maximum grade **/ function webquestscorm_grades($wqid) { if (!($webquestscorm = get_record('webquestscorm', 'id', $wqid))) { return NULL; } if ($webquestscorm->grade == 0) { return NULL; } $grades = get_records_menu('webquestscorm_submissions', 'webquestscorm', $webquestscorm->id, '', 'userid,grade'); if ($webquestscorm->grade > 0) { if ($grades) { foreach ($grades as $userid => $grade) { if ($grade == -1) { $grades[$userid] = '-'; } } } $return->grades = $grades; $return->maxgrade = $webquestscorm->grade; } else { if ($grades) { $scaleid = -$webquestscorm->grade; $maxgrade = ""; if ($scale = get_record('scale', 'id', $scaleid)) { $scalegrades = make_menu_from_list($scale->scale); foreach ($grades as $userid => $grade) { if (empty($scalegrades[$grade])) { $grades[$userid] = '-'; } else { $grades[$userid] = $scalegrades[$grade]; } } $maxgrade = $scale->name; } } $return->grades = $grades; $return->maxgrade = $maxgrade; } return $return; }
/** * Print core grade * @parm optional grade to show * @return string */ function print_grade_core($grade = null) { $ret = ''; $inst = $this->instance; if ($inst->dategraded > 0 || $grade != null) { $vplinstance = $this->vpl->get_instance(); $scaleid = $this->vpl->get_grade(); $options = array(); if ($scaleid == 0) { $ret = get_string('nograde'); } if ($scaleid > 0) { if ($grade == null) { //remove trailing zeros if needed $grade = vpl_rtzeros($inst->grade); } $ret = $grade . ' / ' . $scaleid; } elseif ($scaleid < 0) { $scaleid = -$scaleid; if ($grade === null) { $grade = trim($inst->grade); } $grade = (int) $grade; if ($scale = $this->vpl->get_scale()) { $options = array(); $options[-1] = get_string('nograde'); $options = $options + make_menu_from_list($scale->scale); if (isset($options[$grade])) { $ret = $options[$grade]; } } } } return $ret; }
/** * Creates an array that represents all the current grades that * can be chosen using the given grading type. * * Negative numbers * are scales, zero is no grade, and positive numbers are maximum * grades. * * @todo Finish documenting this function or better deprecated this completely! * * @param int $gradingtype * @return array */ function make_grades_menu($gradingtype) { global $DB; $grades = array(); if ($gradingtype < 0) { if ($scale = $DB->get_record('scale', array('id' => -$gradingtype))) { return make_menu_from_list($scale->scale); } } else { if ($gradingtype > 0) { for ($i = $gradingtype; $i >= 0; $i--) { $grades[$i] = $i . ' / ' . $gradingtype; } return $grades; } } return $grades; }
/** * Must return an array of grades for a given instance of this module, * indexed by user. It also returns a maximum allowed grade. * * Example: * $return->grades = array of grades; * $return->maxgrade = maximum allowed grade; * * return $return; * * @param int $newmoduleid ID of an instance of this module * @return mixed null or object with an array of grades and with the maximum grade **/ function brainstorm_grades($cmid) { global $CFG; if (!($module = get_record('course_modules', 'id', $cmid))) { return NULL; } if (!($brainstorm = get_record('brainstorm', 'id', $module->instance))) { return NULL; } if ($brainstorm->scale == 0) { // No grading return NULL; } $context = get_context_instance(CONTEXT_MODULE, $cmid); $participants = get_users_by_capability($context, 'mod/brainstorm:gradable', 'u.id,lastname,firstname', 'lastname'); if ($participants) { foreach ($participants as $participant) { $gradeset = brainstorm_get_gradeset($brainstorm->id, $participant->id); if (!$gradeset) { return null; } if ($brainstorm->scale > 0) { // Grading numerically if ($brainstorm->singlegrade) { $finalgrades[$participant->id] = $gradeset->single; } else { if ($brainstorm->seqaccesscollect && isset($gradeset->participate)) { $total[] = $gradeset->participate; $weights[] = $brainstorm->participationweight; } if ($brainstorm->seqaccessprepare && isset($gradeset->prepare)) { $total[] = $gradeset->prepare; $weights[] = $brainstorm->preparingweight; } if ($brainstorm->seqaccessorganize && isset($gradeset->organize)) { $total[] = $gradeset->organize; $weights[] = $brainstorm->organizeweight; } if ($brainstorm->seqaccessfeedback && isset($gradeset->feedback)) { $total[] = $gradeset->feedback; $weights[] = $brainstorm->feedbackweight; } $totalweights = array_sum($weights); $totalgrade = 0; for ($i = 0; $i < count(@$total); $i++) { $totalgrade += $total[$i] * $weights[$i]; } $totalgrade = $totalweights != 0 ? round($totalgrade / $totalweights) : 0; $finalgrades[$participant->id] = $totalgrade; } $return->grades = @$finalgrades; $return->maxgrade = $brainstorm->scale; return $return; } else { // Scales $finalgrades = array(); $scaleid = -$brainstorm->grade; $maxgrade = ''; if ($scale = get_record('scale', 'id', $scaleid)) { $scalegrades = make_menu_from_list($scale->scale); } if ($brainstorm->singlegrade) { $finalgrades[$participant->id] = $scalegrades($gradeset->single); } else { if ($brainstorm->setaccesscollect) { $total[] = $scalegrades($gradeset->participate); $weights[] = $brainstorm->participationweight; } if ($brainstorm->setaccessprepare) { $total[] = $scalegrades($gradeset->prepare); $weights[] = $brainstorm->preparingweight; } if ($brainstorm->setaccessorganize) { $total[] = $scalegrades($gradeset->organize); $weights[] = $brainstorm->organizeweight; } if ($brainstorm->setaccessfeedback) { $total[] = $scalegrades($gradeset->feedback); $weights[] = $brainstorm->feedbackweight; } $totalweights = array_sum($weights); $totalgrade = 0; for ($i = 0; $i < count(@$total); $i++) { $totalgrade += $total[$i] * $weights[$i]; } $totalgrade = $totalweights != 0 ? round($totalgrade / $totalweights) : 0; $finalgrades[$participant->id] = $totalgrade; } $return->grades = @$final; $return->maxgrade = $maxgrade; return $return; } } } return null; }
/** * Update a grade in the grade table for the assignment and in the gradebook. * * @param stdClass $grade a grade record keyed on id * @return bool true for success */ public function update_grade($grade) { global $DB; $grade->timemodified = time(); if (!empty($grade->workflowstate)) { $validstates = $this->get_marking_workflow_states_for_current_user(); if (!array_key_exists($grade->workflowstate, $validstates)) { return false; } } if ($grade->grade && $grade->grade != -1) { if ($this->get_instance()->grade > 0) { if (!is_numeric($grade->grade)) { return false; } else { if ($grade->grade > $this->get_instance()->grade) { return false; } else { if ($grade->grade < 0) { return false; } } } } else { // This is a scale. if ($scale = $DB->get_record('scale', array('id' => -$this->get_instance()->grade))) { $scaleoptions = make_menu_from_list($scale->scale); if (!array_key_exists((int) $grade->grade, $scaleoptions)) { return false; } } } } if (empty($grade->attemptnumber)) { // Set it to the default. $grade->attemptnumber = 0; } $result = $DB->update_record('assign_grades', $grade); // Only push to gradebook if the update is for the latest attempt. $submission = null; if ($this->get_instance()->teamsubmission) { $submission = $this->get_group_submission($grade->userid, 0, false); } else { $submission = $this->get_user_submission($grade->userid, false); } // Not the latest attempt. if ($submission && $submission->attemptnumber != $grade->attemptnumber) { return true; } if ($result) { $this->gradebook_item_update(null, $grade); } return $result; }
function netpublish_grades($netpublishid) { /// Must return an array of grades for a given instance of this module, /// indexed by user. It also returns a maximum allowed grade. $retval = new stdClass(); $netpublishid = clean_param($netpublishid, PARAM_INT); if (!($netpublish = get_record("netpublish", "id", $netpublishid))) { return NULL; } $grades = get_records_menu("netpublish_grades", "publishid", $netpublish->id, "", "userid,grade"); if ($netpublish->scale > 0) { $retval->grades = $grades; $retval->maxgrade = $netpublish->scale; } else { if ($netpublish->scale == 0) { return NULL; } else { if ($scale = get_record("scale", "id", -$netpublish->scale)) { $scalegrades = make_menu_from_list($scale->scale); if ($grades) { foreach ($grades as $key => $grade) { $grades[$key] = $scalegrades[$grade]; } } } $retval->grades = $grades; $retval->maxgrade = ""; } } return $retval; }