/** * Outputs the part form HTML * @param object $cm The moodle course module object for this instance * @param object $part The moodle course module object for this instance * @return string Return the part form HTML output */ function turnitintool_partform($cm, $part) { global $CFG, $OUTPUT; $output = '<form name="partform" method="POST" action="' . $CFG->wwwroot . '/mod/turnitintool/view.php' . '?id=' . $cm->id . '&do=intro">' . PHP_EOL; $element = new MoodleQuickForm_hidden('submitted'); $element->setValue($part->id); $output .= $element->toHTML() . PHP_EOL; $table = new stdClass(); $table->width = '100%'; $table->id = 'uploadtableid'; $table->class = 'uploadtable'; // Part Name Field unset($cells); $cells[0] = new stdClass(); $cells[0]->data = get_string('partname', 'turnitintool'); $cells[0]->class = 'cell c0'; $attr = array('class' => "formwide"); $element = new MoodleQuickForm_text('partname', null, $attr); $element->setValue($part->partname); $cells[1] = new stdClass(); $cells[1]->data = $element->toHTML(); $cells[1]->class = 'cell c1'; $table->rows[0] = new stdClass(); $table->rows[0]->cells = $cells; $dateoptions = array('startyear' => date('Y', strtotime('-6 years')), 'stopyear' => date('Y', strtotime('+6 years')), 'timezone' => 99, 'applydst' => true, 'step' => 1, 'optional' => false); // Part Start Date unset($cells); $cells[0] = new stdClass(); $cells[0]->data = get_string('dtstart', 'turnitintool'); $cells[0]->class = 'cell c0'; $element = new MoodleQuickForm_date_time_selector('dtstart', null, $dateoptions); $date = array('hour' => userdate($part->dtstart, '%H'), 'minute' => userdate($part->dtstart, '%M'), 'day' => userdate($part->dtstart, '%d'), 'month' => userdate($part->dtstart, '%m'), 'year' => userdate($part->dtstart, '%Y')); $element->setValue($date); $cells[1] = new stdClass(); $cells[1]->data = $element->toHTML(); $cells[1]->class = 'cell c1'; $table->rows[1] = new stdClass(); $table->rows[1]->cells = $cells; // Part Due Date unset($cells); $cells[0] = new stdClass(); $cells[0]->data = get_string('dtdue', 'turnitintool'); $cells[0]->class = 'cell c0'; $element = new MoodleQuickForm_date_time_selector('dtdue', null, $dateoptions); $date = array('hour' => userdate($part->dtdue, '%H'), 'minute' => userdate($part->dtdue, '%M'), 'day' => userdate($part->dtdue, '%d'), 'month' => userdate($part->dtdue, '%m'), 'year' => userdate($part->dtdue, '%Y')); $element->setValue($date); $cells[1] = new stdClass(); $cells[1]->data = $element->toHTML(); $cells[1]->class = 'cell c1'; $table->rows[2] = new stdClass(); $table->rows[2]->cells = $cells; // Part Post Date unset($cells); $cells[0] = new stdClass(); $cells[0]->data = get_string('dtpost', 'turnitintool'); $cells[0]->class = 'cell c0'; $element = new MoodleQuickForm_date_time_selector('dtpost', null, $dateoptions); $date = array('hour' => userdate($part->dtpost, '%H'), 'minute' => userdate($part->dtpost, '%M'), 'day' => userdate($part->dtpost, '%d'), 'month' => userdate($part->dtpost, '%m'), 'year' => userdate($part->dtpost, '%Y')); $element->setValue($date); $cells[1] = new stdClass(); $cells[1]->data = $element->toHTML(); $cells[1]->class = 'cell c1'; $table->rows[3] = new stdClass(); $table->rows[3]->cells = $cells; // Part Max Marks unset($cells); $cells[0] = new stdClass(); $cells[0]->data = get_string('maxmarks', 'turnitintool'); $cells[0]->class = 'cell c0'; $element = new MoodleQuickForm_text('maxmarks'); $element->setValue($part->maxmarks); $cells[1] = new stdClass(); $cells[1]->data = $element->toHTML(); $cells[1]->class = 'cell c1'; $table->rows[4] = new stdClass(); $table->rows[4]->cells = $cells; // Submit / Cancel unset($cells); $cells[0] = new stdClass(); $cells[0]->data = ' '; $cells[0]->class = 'cell c0'; $attr = array('onclick' => "location.href='" . $CFG->wwwroot . "/mod/turnitintool/view.php?id=" . $cm->id . "';"); $element = new MoodleQuickForm_button('cancel', 'Cancel', $attr); $cells[1] = new stdClass(); $cells[1]->data = $element->toHTML(); $element = new MoodleQuickForm_submit('submit', 'Submit'); $cells[1]->data .= $element->toHTML(); $cells[1]->class = 'cell c1'; $table->rows[5] = new stdClass(); $table->rows[5]->cells = $cells; $output .= turnitintool_print_table($table, true); $output .= '</form>' . PHP_EOL; return $output; }
/** * Called by HTML_QuickForm whenever form event is made on this element. * * @param string $event Name of event * @param mixed $arg event arguments * @param moodleform $caller calling object * @return mixed */ public function onQuickFormEvent($event, $arg, &$caller) { switch ($event) { case 'createElement': // The first argument is the name. $name = $arg[0]; // Set disable actions. $caller->disabledIf($name . '[modgrade_scale]', $name . '[modgrade_type]', 'neq', 'scale'); $caller->disabledIf($name . '[modgrade_point]', $name . '[modgrade_type]', 'neq', 'point'); $caller->disabledIf($name . '[modgrade_rescalegrades]', $name . '[modgrade_type]', 'neq', 'point'); // Set validation rules for the sub-elements belonging to this element. // A handy note: the parent scope of a closure is the function in which the closure was declared. // Because of this using $this is safe despite the closures being called statically. // A nasty magic hack! $checkgradetypechange = function ($val) { // Nothing is affected by changes to the grade type if there are no grades yet. if (!$this->hasgrades) { return true; } // Check if we are changing the grade type when grades are present. if (isset($val['modgrade_type']) && $val['modgrade_type'] !== $this->currentgradetype) { return false; } return true; }; $checkscalechange = function ($val) { // Nothing is affected by changes to the scale if there are no grades yet. if (!$this->hasgrades) { return true; } // Check if we are changing the scale type when grades are present. if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'scale') { if (isset($val['modgrade_scale']) && $val['modgrade_scale'] !== $this->currentscaleid) { return false; } } return true; }; $checkmaxgradechange = function ($val) { // Nothing is affected by changes to the max grade if there are no grades yet. if (!$this->hasgrades) { return true; } // If we are not using ratings we can change the max grade. if (!$this->useratings) { return true; } // Check if we are changing the max grade if we are using ratings and there is a grade. if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'point') { if (isset($val['modgrade_point']) && grade_floats_different($this->currentgrade, $val['modgrade_point'])) { return false; } } return true; }; $checkmaxgrade = function ($val) { // Closure to validate a max points value. See the note above about scope if this confuses you. if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'point') { if (!isset($val['modgrade_point'])) { return false; } return $this->validate_point($val['modgrade_point']); } return true; }; $checkvalidscale = function ($val) { // Closure to validate a scale value. See the note above about scope if this confuses you. if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'scale') { if (!isset($val['modgrade_scale'])) { return false; } return $this->validate_scale($val['modgrade_scale']); } return true; }; $checkrescale = function ($val) { // Nothing is affected by changes to grademax if there are no grades yet. if (!$this->isupdate || !$this->hasgrades || !$this->canrescale) { return true; } // Closure to validate a scale value. See the note above about scope if this confuses you. if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'point') { // Work out if the value was actually changed in the form. if (grade_floats_different($this->currentgrade, $val['modgrade_point'])) { if (empty($val['modgrade_rescalegrades'])) { // This was an "edit", the grademax was changed and the process existing setting was not set. return false; } } } return true; }; $cantchangegradetype = get_string('modgradecantchangegradetype', 'grades'); $cantchangemaxgrade = get_string('modgradecantchangeratingmaxgrade', 'grades'); $maxgradeexceeded = get_string('modgradeerrorbadpoint', 'grades', get_config('core', 'gradepointmax')); $invalidscale = get_string('modgradeerrorbadscale', 'grades'); $cantchangescale = get_string('modgradecantchangescale', 'grades'); $mustchooserescale = get_string('mustchooserescaleyesorno', 'grades'); // When creating the rules the sixth arg is $force, we set it to true because otherwise the form // will attempt to validate the existence of the element, we don't want this because the element // is being created right now and doesn't actually exist as a registered element yet. $caller->addRule($name, $cantchangegradetype, 'callback', $checkgradetypechange, 'server', false, true); $caller->addRule($name, $cantchangemaxgrade, 'callback', $checkmaxgradechange, 'server', false, true); $caller->addRule($name, $maxgradeexceeded, 'callback', $checkmaxgrade, 'server', false, true); $caller->addRule($name, $invalidscale, 'callback', $checkvalidscale, 'server', false, true); $caller->addRule($name, $cantchangescale, 'callback', $checkscalechange, 'server', false, true); $caller->addRule($name, $mustchooserescale, 'callback', $checkrescale, 'server', false, true); break; case 'updateValue': // As this is a group element with no value of its own we are only interested in situations where the // default value or a constant value are being provided to the actual element. // In this case we expect an int that is going to translate to a scale if negative, or to max points // if positive. // Set the maximum points field to disabled if the rescale option has not been chosen and there are grades. $caller->disabledIf($this->getName() . '[modgrade_point]', $this->getName() . '[modgrade_rescalegrades]', 'eq', ''); // A constant value should be given as an int. // The default value should be an int and should really be $CFG->gradepointdefault. $value = $this->_findValue($caller->_constantValues); if (null === $value) { if ($caller->isSubmitted()) { break; } $value = $this->_findValue($caller->_defaultValues); } if (!is_null($value) && !is_scalar($value)) { // Something unexpected (likely an array of subelement values) has been given - this will be dealt // with somewhere else - where exactly... likely the subelements. debugging('An invalid value (type ' . gettype($value) . ') has arrived at ' . __METHOD__, DEBUG_DEVELOPER); break; } // Set element state for existing data. // This is really a pretty hacky thing to do, when data is being set the group element is called // with the data first and the subelements called afterwards. // This means that the subelements data (inc const and default values) can be overridden by form code. // So - when we call this code really we can't be sure that will be the end value for the element. if (!empty($this->_elements)) { if (!empty($value)) { if ($value < 0) { $this->gradetypeformelement->setValue('scale'); $this->scaleformelement->setValue($value * -1); } else { if ($value > 0) { $this->gradetypeformelement->setValue('point'); $this->maxgradeformelement->setValue($value); } } } else { $this->gradetypeformelement->setValue('none'); $this->maxgradeformelement->setValue(''); } } break; } // Always let the parent do its thing! return parent::onQuickFormEvent($event, $arg, $caller); }