function can_do_edit() { $association_id = 0; if (!empty($this->params['association_id'])) { $association_id = $this->params['association_id']; } else { $association_id = $this->optional_param('association_id', '', PARAM_INT); } $student = new student($association_id); return student::can_manage_assoc($student->userid, $student->classid); }
/** * Attempt initial enrolment. * * This performs an initial attempt at enroling the selected users. This has not yet taken into account the enrolment limit * or permissions. * * @param array $elements An array of elements to perform the action on. * @param int $classid The ID of the class we're enrolling into. * @param string $enroldata A JSON string containing enrolment data for the users we want to overenrol. * @param bool $bulkaction Whether this attempt is a bulk action or not. * @return array An array consisting of "result", and optionally "users" and "total", explained below: * result: Will be "success" if all users were enrolled successfully, or "waitlist", if we have users that * need to be waitlisted. * users: If some users need enrolment limit resolution, this will be present. * This will either contain an array of arrays like array('userid' => $userid, 'name' => $label), * or the string 'bulklist', if we're performing a bulk action. * total: If we're performing a bulk action, and some users need enrolment limit resolution, this will be * included, indicating the number of users needed resolution. */ protected function attempt_enrolment($elements, $classid, $enroldata, $bulkaction) { set_time_limit(0); // Enrolment data. $enroldata = $this->process_enrolment_data($classid, @json_decode($enroldata)); if (empty($enroldata)) { throw new Exception('Did not receive valid enrolment data.'); } // Attempt enrolment. $waitlist = array(); foreach ($elements as $userid => $label) { // Skip invalid userids or users which we dont have permission to modify. if (!is_numeric($userid) || !student::can_manage_assoc($userid, $classid)) { continue; } // Build student. $sturecord = $enroldata; $sturecord['userid'] = $userid; $newstu = new student($sturecord); $newstu->validation_overrides[] = 'prerequisites'; if ($newstu->completestatusid != STUSTATUS_NOTCOMPLETE) { // User is set to completed, so don't worry about enrolment limit. $newstu->validation_overrides[] = 'enrolment_limit'; } // Attempt enrolment. try { $newstu->save(); unset($elements[$userid]); $this->datatable->bulklist_modify(array(), array($userid)); } catch (pmclass_enrolment_limit_validation_exception $e) { $waitlist[] = array('userid' => $userid, 'name' => $label); } catch (Exception $e) { $param = array('message' => $e->getMessage()); throw new Exception(get_string('record_not_created_reason', 'local_elisprogram', $param)); } } if ($bulkaction === true) { if (!empty($waitlist)) { list($bulklistdisplay, $totalusers) = $this->datatable->bulklist_get_display(1); return array('result' => 'waitlist', 'users' => 'bulklist', 'total' => $totalusers); } else { return array('result' => 'success'); } } else { return !empty($waitlist) ? array('result' => 'waitlist', 'users' => $waitlist) : array('result' => 'success'); } }
/** * Perform an update for a single user/class pair. * * @param int $userid The user ID we're updating. * @param int $classid The class ID we're updating information for. * @param array $enroldata The updated enrolment data. * @param array $learningobjectives The updated learning objective data. */ protected function do_update($userid, $classid, array $enroldata, array $learningobjectives) { global $DB; if (student::can_manage_assoc($userid, $classid) !== true) { throw new Exception('Unauthorized'); } if (!isset($enroldata['id'])) { $associationid = $DB->get_field(student::TABLE, 'id', array('classid' => $classid, 'userid' => $userid)); if (empty($associationid)) { return false; } else { $enroldata['id'] = $associationid; } } $enroldata['userid'] = $userid; $stu = new student($enroldata); if ($stu->completestatusid == STUSTATUS_PASSED && $DB->get_field(student::TABLE, 'completestatusid', array('id' => $stu->id)) != STUSTATUS_PASSED) { $stu->complete(); } else { $status = $stu->save(); } foreach ($learningobjectives as $id => $data) { $graderec = array('userid' => $userid, 'classid' => $classid, 'completionid' => $id); $existingrec = $DB->get_record(student_grade::TABLE, $graderec); if (!empty($existingrec)) { $graderec = (array) $existingrec; } $graderec['timegraded'] = $data['timegraded']; $graderec['grade'] = $data['grade']; $graderec['locked'] = $data['locked']; $sgrade = new student_grade($graderec); $sgrade->save(); } }