/** * The constructor function calls the abstract function definition() and it will then * process and clean and attempt to validate incoming data. * * It will call your custom validate method to validate data and will also check any rules * you have specified in definition using addRule * * The name of the form (id attribute of the form) is automatically generated depending on * the name you gave the class extending moodleform. You should call your class something * like * * @param mixed $action the action attribute for the form. If empty defaults to auto detect the * current url. If a moodle_url object then outputs params as hidden variables. * @param array $customdata if your form defintion method needs access to data such as $course * $cm, etc. to construct the form definition then pass it in this array. You can * use globals for somethings. * @param string $method if you set this to anything other than 'post' then _GET and _POST will * be merged and used as incoming data to the form. * @param string $target target frame for form submission. You will rarely use this. Don't use * it if you don't need to as the target attribute is deprecated in xhtml * strict. * @param mixed $attributes you can pass a string of html attributes here or an array. * @return moodleform */ function moodleform($action = null, $customdata = null, $method = 'post', $target = '', $attributes = null, $editable = true) { if (empty($action)) { $action = strip_querystring(qualified_me()); } $this->_formname = get_class($this); // '_form' suffix kept in order to prevent collisions of form id and other element $this->_customdata = $customdata; $this->_form =& new MoodleQuickForm($this->_formname, $method, $action, $target, $attributes); if (!$editable) { $this->_form->hardFreeze(); } $this->set_upload_manager(new upload_manager()); $this->definition(); $this->_form->addElement('hidden', 'sesskey', null); // automatic sesskey protection $this->_form->setType('sesskey', PARAM_RAW); $this->_form->setDefault('sesskey', sesskey()); $this->_form->addElement('hidden', '_qf__' . $this->_formname, null); // form submission marker $this->_form->setType('_qf__' . $this->_formname, PARAM_RAW); $this->_form->setDefault('_qf__' . $this->_formname, 1); $this->_form->_setDefaultRuleMessages(); // we have to know all input types before processing submission ;-) $this->_process_submission($method); }
/** * Add elements to grade form. * * @param MoodleQuickForm $mform * @param stdClass $data * @param array $params * @return void */ public function add_grade_form_elements(MoodleQuickForm $mform, stdClass $data, $params) { global $USER, $CFG; $settings = $this->get_instance(); $rownum = $params['rownum']; $last = $params['last']; $useridlistid = $params['useridlistid']; $userid = $params['userid']; $attemptnumber = $params['attemptnumber']; if (!$userid) { $cache = cache::make_from_params(cache_store::MODE_SESSION, 'mod_assign', 'useridlist'); if (!($useridlist = $cache->get($this->get_course_module()->id . '_' . $useridlistid))) { $useridlist = $this->get_grading_userid_list(); $cache->set($this->get_course_module()->id . '_' . $useridlistid, $useridlist); } } else { $useridlist = array($userid); $rownum = 0; $useridlistid = ''; } $userid = $useridlist[$rownum]; $grade = $this->get_user_grade($userid, false, $attemptnumber); $submission = null; if ($this->get_instance()->teamsubmission) { $submission = $this->get_group_submission($userid, 0, false, $attemptnumber); } else { $submission = $this->get_user_submission($userid, false, $attemptnumber); } // Add advanced grading. $gradingdisabled = $this->grading_disabled($userid); $gradinginstance = $this->get_grading_instance($userid, $grade, $gradingdisabled); $mform->addElement('header', 'gradeheader', get_string('grade')); if ($gradinginstance) { $gradingelement = $mform->addElement('grading', 'advancedgrading', get_string('grade') . ':', array('gradinginstance' => $gradinginstance)); if ($gradingdisabled) { $gradingelement->freeze(); } else { $mform->addElement('hidden', 'advancedgradinginstanceid', $gradinginstance->get_id()); $mform->setType('advancedgradinginstanceid', PARAM_INT); } } else { // Use simple direct grading. if ($this->get_instance()->grade > 0) { $name = get_string('gradeoutof', 'assign', $this->get_instance()->grade); if (!$gradingdisabled) { $gradingelement = $mform->addElement('text', 'grade', $name); $mform->addHelpButton('grade', 'gradeoutofhelp', 'assign'); $mform->setType('grade', PARAM_RAW); } else { $mform->addElement('hidden', 'grade', $name); $mform->hardFreeze('grade'); $mform->setType('grade', PARAM_RAW); $strgradelocked = get_string('gradelocked', 'assign'); $mform->addElement('static', 'gradedisabled', $name, $strgradelocked); $mform->addHelpButton('gradedisabled', 'gradeoutofhelp', 'assign'); } } else { $grademenu = array(-1 => get_string("nograde")) + make_grades_menu($this->get_instance()->grade); if (count($grademenu) > 1) { $gradingelement = $mform->addElement('select', 'grade', get_string('grade') . ':', $grademenu); // The grade is already formatted with format_float so it needs to be converted back to an integer. if (!empty($data->grade)) { $data->grade = (int) unformat_float($data->grade); } $mform->setType('grade', PARAM_INT); if ($gradingdisabled) { $gradingelement->freeze(); } } } } $gradinginfo = grade_get_grades($this->get_course()->id, 'mod', 'assign', $this->get_instance()->id, $userid); if (!empty($CFG->enableoutcomes)) { foreach ($gradinginfo->outcomes as $index => $outcome) { $options = make_grades_menu(-$outcome->scaleid); if ($outcome->grades[$userid]->locked) { $options[0] = get_string('nooutcome', 'grades'); $mform->addElement('static', 'outcome_' . $index . '[' . $userid . ']', $outcome->name . ':', $options[$outcome->grades[$userid]->grade]); } else { $options[''] = get_string('nooutcome', 'grades'); $attributes = array('id' => 'menuoutcome_' . $index); $mform->addElement('select', 'outcome_' . $index . '[' . $userid . ']', $outcome->name . ':', $options, $attributes); $mform->setType('outcome_' . $index . '[' . $userid . ']', PARAM_INT); $mform->setDefault('outcome_' . $index . '[' . $userid . ']', $outcome->grades[$userid]->grade); } } } $capabilitylist = array('gradereport/grader:view', 'moodle/grade:viewall'); if (has_all_capabilities($capabilitylist, $this->get_course_context())) { $urlparams = array('id' => $this->get_course()->id); $url = new moodle_url('/grade/report/grader/index.php', $urlparams); $usergrade = '-'; if (isset($gradinginfo->items[0]->grades[$userid]->str_grade)) { $usergrade = $gradinginfo->items[0]->grades[$userid]->str_grade; } $gradestring = $this->get_renderer()->action_link($url, $usergrade); } else { $usergrade = '-'; if (isset($gradinginfo->items[0]->grades[$userid]) && !$gradinginfo->items[0]->grades[$userid]->hidden) { $usergrade = $gradinginfo->items[0]->grades[$userid]->str_grade; } $gradestring = $usergrade; } if ($this->get_instance()->markingworkflow) { $states = $this->get_marking_workflow_states_for_current_user(); $options = array('' => get_string('markingworkflowstatenotmarked', 'assign')) + $states; $mform->addElement('select', 'workflowstate', get_string('markingworkflowstate', 'assign'), $options); $mform->addHelpButton('workflowstate', 'markingworkflowstate', 'assign'); } if ($this->get_instance()->markingallocation && has_capability('mod/assign:manageallocations', $this->context)) { $markers = get_users_by_capability($this->context, 'mod/assign:grade'); $markerlist = array('' => get_string('choosemarker', 'assign')); foreach ($markers as $marker) { $markerlist[$marker->id] = fullname($marker); } $mform->addElement('select', 'allocatedmarker', get_string('allocatedmarker', 'assign'), $markerlist); $mform->addHelpButton('allocatedmarker', 'allocatedmarker', 'assign'); $mform->disabledIf('allocatedmarker', 'workflowstate', 'eq', ASSIGN_MARKING_WORKFLOW_STATE_READYFORREVIEW); $mform->disabledIf('allocatedmarker', 'workflowstate', 'eq', ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW); $mform->disabledIf('allocatedmarker', 'workflowstate', 'eq', ASSIGN_MARKING_WORKFLOW_STATE_READYFORRELEASE); $mform->disabledIf('allocatedmarker', 'workflowstate', 'eq', ASSIGN_MARKING_WORKFLOW_STATE_RELEASED); } $mform->addElement('static', 'currentgrade', get_string('currentgrade', 'assign'), $gradestring); if (count($useridlist) > 1) { $strparams = array('current' => $rownum + 1, 'total' => count($useridlist)); $name = get_string('outof', 'assign', $strparams); $mform->addElement('static', 'gradingstudent', get_string('gradingstudent', 'assign'), $name); } // Let feedback plugins add elements to the grading form. $this->add_plugin_grade_elements($grade, $mform, $data, $userid); // Hidden params. $mform->addElement('hidden', 'id', $this->get_course_module()->id); $mform->setType('id', PARAM_INT); $mform->addElement('hidden', 'rownum', $rownum); $mform->setType('rownum', PARAM_INT); $mform->setConstant('rownum', $rownum); $mform->addElement('hidden', 'useridlistid', $useridlistid); $mform->setType('useridlistid', PARAM_INT); $mform->addElement('hidden', 'attemptnumber', $attemptnumber); $mform->setType('attemptnumber', PARAM_INT); $mform->addElement('hidden', 'ajax', optional_param('ajax', 0, PARAM_INT)); $mform->setType('ajax', PARAM_INT); if ($this->get_instance()->teamsubmission) { $mform->addElement('header', 'groupsubmissionsettings', get_string('groupsubmissionsettings', 'assign')); $mform->addElement('selectyesno', 'applytoall', get_string('applytoteam', 'assign')); $mform->setDefault('applytoall', 1); } // Do not show if we are editing a previous attempt. if ($attemptnumber == -1 && $this->get_instance()->attemptreopenmethod != ASSIGN_ATTEMPT_REOPEN_METHOD_NONE) { $mform->addElement('header', 'attemptsettings', get_string('attemptsettings', 'assign')); $attemptreopenmethod = get_string('attemptreopenmethod_' . $this->get_instance()->attemptreopenmethod, 'assign'); $mform->addElement('static', 'attemptreopenmethod', get_string('attemptreopenmethod', 'assign'), $attemptreopenmethod); $attemptnumber = 0; if ($submission) { $attemptnumber = $submission->attemptnumber; } $maxattempts = $this->get_instance()->maxattempts; if ($maxattempts == ASSIGN_UNLIMITED_ATTEMPTS) { $maxattempts = get_string('unlimitedattempts', 'assign'); } $mform->addelement('static', 'maxattemptslabel', get_string('maxattempts', 'assign'), $maxattempts); $mform->addelement('static', 'attemptnumberlabel', get_string('attemptnumber', 'assign'), $attemptnumber + 1); $ismanual = $this->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL; $issubmission = !empty($submission); $isunlimited = $this->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS; $islessthanmaxattempts = $issubmission && $submission->attemptnumber < $this->get_instance()->maxattempts - 1; if ($ismanual && (!$issubmission || $isunlimited || $islessthanmaxattempts)) { $mform->addElement('selectyesno', 'addattempt', get_string('addattempt', 'assign')); $mform->setDefault('addattempt', 0); } } $mform->addElement('hidden', 'action', 'submitgrade'); $mform->setType('action', PARAM_ALPHA); $buttonarray = array(); $name = get_string('savechanges', 'assign'); $buttonarray[] = $mform->createElement('submit', 'savegrade', $name); if (!$last) { $name = get_string('savenext', 'assign'); $buttonarray[] = $mform->createElement('submit', 'saveandshownext', $name); } $buttonarray[] = $mform->createElement('cancel', 'cancelbutton', get_string('cancel')); $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); $mform->closeHeaderBefore('buttonar'); $buttonarray = array(); if ($rownum > 0) { $name = get_string('previous', 'assign'); $buttonarray[] = $mform->createElement('submit', 'nosaveandprevious', $name); } if (!$last) { $name = get_string('nosavebutnext', 'assign'); $buttonarray[] = $mform->createElement('submit', 'nosaveandnext', $name); } if (!empty($buttonarray)) { $mform->addGroup($buttonarray, 'navar', '', array(' '), false); } // The grading form does not work well with shortforms. $mform->setDisableShortforms(); }
/** * Adds enrol instance UI to course edit form * * @param object $instance enrol instance or null if does not exist yet * @param MoodleQuickForm $mform * @param object $data * @param object $context context of existing course or parent category if course does not exist * @return void */ public function course_edit_form($instance, MoodleQuickForm $mform, $data, $context) { $i = isset($instance->id) ? $instance->id : 0; if (!$i and !$this->get_config('defaultenrol')) { return; } $header = $this->get_instance_name($instance); $config = has_capability('enrol/guest:config', $context); $mform->addElement('header', 'enrol_guest_header_' . $i, $header); $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'), ENROL_INSTANCE_DISABLED => get_string('no')); $mform->addElement('select', 'enrol_guest_status_' . $i, get_string('status', 'enrol_guest'), $options); $mform->addHelpButton('enrol_guest_status_' . $i, 'status', 'enrol_guest'); $mform->setDefault('enrol_guest_status_' . $i, $this->get_config('status')); $mform->setAdvanced('enrol_guest_status_' . $i, $this->get_config('status_adv')); if (!$config) { $mform->hardFreeze('enrol_guest_status_' . $i); } $mform->addElement('passwordunmask', 'enrol_guest_password_' . $i, get_string('password', 'enrol_guest')); $mform->addHelpButton('enrol_guest_password_' . $i, 'password', 'enrol_guest'); if (!$config) { $mform->hardFreeze('enrol_guest_password_' . $i); } else { $mform->disabledIf('enrol_guest_password_' . $i, 'enrol_guest_status_' . $i, 'noteq', ENROL_INSTANCE_ENABLED); } // now add all values from enrol table if ($instance) { foreach ($instance as $key => $val) { $data->{'enrol_guest_' . $key . '_' . $i} = $val; } } }
/** * Add elements to the edit instance form. * * @param stdClass $instance * @param MoodleQuickForm $mform * @param context $coursecontext * @return bool */ public function edit_instance_form($instance, MoodleQuickForm $mform, $coursecontext) { global $DB; $mform->addElement('text', 'name', get_string('custominstancename', 'enrol')); $mform->setType('name', PARAM_TEXT); $options = $this->get_status_options(); $mform->addElement('select', 'status', get_string('status', 'enrol_cohort'), $options); $options = $this->get_cohort_options($instance, $coursecontext); $mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $options); if ($instance->id) { $mform->setConstant('customint1', $instance->customint1); $mform->hardFreeze('customint1', $instance->customint1); } else { $mform->addRule('customint1', get_string('required'), 'required', null, 'client'); } $roles = $this->get_role_options($instance, $coursecontext); $mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_cohort'), $roles); $mform->setDefault('roleid', $this->get_config('roleid')); $groups = $this->get_group_options($coursecontext); $mform->addElement('select', 'customint2', get_string('addgroup', 'enrol_cohort'), $groups); }