2007-07-29 23:02:03 +00:00
|
|
|
<?php //$Id$
|
|
|
|
|
2007-10-10 06:34:20 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// NOTICE OF COPYRIGHT //
|
|
|
|
// //
|
|
|
|
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
|
|
|
|
// http://moodle.com //
|
|
|
|
// //
|
|
|
|
// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License for more details: //
|
|
|
|
// //
|
|
|
|
// http://www.gnu.org/copyleft/gpl.html //
|
|
|
|
// //
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-07-29 23:02:03 +00:00
|
|
|
require_once $CFG->libdir.'/formslib.php';
|
|
|
|
|
|
|
|
class edit_scale_form extends moodleform {
|
|
|
|
function definition() {
|
|
|
|
global $CFG;
|
|
|
|
$mform =& $this->_form;
|
|
|
|
|
|
|
|
// visible elements
|
|
|
|
$mform->addElement('header', 'general', get_string('scale'));
|
|
|
|
|
2007-07-31 21:10:20 +00:00
|
|
|
$mform->addElement('text', 'name', get_string('name'), 'size="40"');
|
2007-07-29 23:02:03 +00:00
|
|
|
$mform->addRule('name', get_string('required'), 'required', null, 'client');
|
|
|
|
$mform->setType('name', PARAM_TEXT);
|
|
|
|
|
2007-07-30 22:56:45 +00:00
|
|
|
$mform->addElement('advcheckbox', 'standard', get_string('scalestandard'));
|
2008-03-03 10:25:37 +00:00
|
|
|
$mform->setHelpButton('standard', array('scalestandard', get_string('scalestandard'), 'grade'));
|
2007-07-29 23:02:03 +00:00
|
|
|
|
2007-09-18 18:37:58 +00:00
|
|
|
$mform->addElement('static', 'used', get_string('used'));
|
2007-07-29 23:02:03 +00:00
|
|
|
|
|
|
|
$mform->addElement('textarea', 'scale', get_string('scale'), array('cols'=>50, 'rows'=>2));
|
2007-08-08 09:09:46 +00:00
|
|
|
$mform->setHelpButton('scale', array('scales', get_string('scale')));
|
2007-07-29 23:02:03 +00:00
|
|
|
$mform->addRule('scale', get_string('required'), 'required', null, 'client');
|
|
|
|
$mform->setType('scale', PARAM_TEXT);
|
|
|
|
|
|
|
|
$mform->addElement('htmleditor', 'description', get_string('description'), array('cols'=>80, 'rows'=>20));
|
|
|
|
|
|
|
|
|
|
|
|
// hidden params
|
|
|
|
$mform->addElement('hidden', 'id', 0);
|
|
|
|
$mform->setType('id', PARAM_INT);
|
|
|
|
|
|
|
|
$mform->addElement('hidden', 'courseid', 0);
|
|
|
|
$mform->setType('courseid', PARAM_INT);
|
|
|
|
|
|
|
|
/// add return tracking info
|
|
|
|
$gpr = $this->_customdata['gpr'];
|
|
|
|
$gpr->add_mform_elements($mform);
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------
|
|
|
|
// buttons
|
|
|
|
$this->add_action_buttons();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// tweak the form - depending on existing data
|
|
|
|
function definition_after_data() {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$mform =& $this->_form;
|
|
|
|
|
2007-07-30 22:56:45 +00:00
|
|
|
$courseid = $mform->getElementValue('courseid');
|
|
|
|
|
2007-07-29 23:02:03 +00:00
|
|
|
if ($id = $mform->getElementValue('id')) {
|
|
|
|
$scale = grade_scale::fetch(array('id'=>$id));
|
2007-09-18 18:37:58 +00:00
|
|
|
$used = $scale->is_used();
|
2007-07-30 22:56:45 +00:00
|
|
|
|
2007-09-18 18:37:58 +00:00
|
|
|
if ($used) {
|
2007-07-29 23:02:03 +00:00
|
|
|
$mform->hardFreeze('scale');
|
|
|
|
}
|
2007-07-30 22:56:45 +00:00
|
|
|
|
|
|
|
if (empty($courseid)) {
|
|
|
|
$mform->hardFreeze('standard');
|
|
|
|
|
|
|
|
} else if (empty($scale->courseid) and !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) {
|
|
|
|
$mform->hardFreeze('standard');
|
|
|
|
|
2007-09-18 18:37:58 +00:00
|
|
|
} else if ($used and !empty($scale->courseid)) {
|
2007-07-30 22:56:45 +00:00
|
|
|
$mform->hardFreeze('standard');
|
|
|
|
}
|
|
|
|
|
2007-09-18 18:37:58 +00:00
|
|
|
$usedstr = $scale->is_used() ? get_string('yes') : get_string('no');
|
|
|
|
$used_el =& $mform->getElement('used');
|
|
|
|
$used_el->setValue($usedstr);
|
2007-07-29 23:02:03 +00:00
|
|
|
|
|
|
|
} else {
|
2007-09-18 18:37:58 +00:00
|
|
|
$mform->removeElement('used');
|
2007-07-30 22:56:45 +00:00
|
|
|
if (empty($courseid) or !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) {
|
|
|
|
$mform->hardFreeze('standard');
|
2007-07-29 23:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// perform extra validation before submission
|
2007-11-23 22:15:07 +00:00
|
|
|
function validation($data, $files) {
|
2008-06-01 17:53:25 +00:00
|
|
|
global $CFG, $COURSE, $DB;
|
2007-08-01 21:48:10 +00:00
|
|
|
|
2007-11-23 22:15:07 +00:00
|
|
|
$errors = parent::validation($data, $files);
|
2007-08-01 21:48:10 +00:00
|
|
|
|
2007-08-01 07:40:40 +00:00
|
|
|
// we can not allow 2 scales with the same exact scale as this creates
|
|
|
|
// problems for backup/restore
|
2007-09-15 21:17:11 +00:00
|
|
|
|
|
|
|
$old = grade_scale::fetch(array('id'=>$data['id']));
|
|
|
|
|
|
|
|
if (array_key_exists('standard', $data)) {
|
|
|
|
if (empty($data['standard'])) {
|
|
|
|
$courseid = $COURSE->id;
|
|
|
|
} else {
|
|
|
|
$courseid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$courseid = $old->courseid;
|
2007-08-01 21:48:10 +00:00
|
|
|
}
|
2007-07-29 23:02:03 +00:00
|
|
|
|
2007-09-15 21:17:11 +00:00
|
|
|
if (array_key_exists('scale', $data)) {
|
2008-06-01 17:53:25 +00:00
|
|
|
$count = $DB->count_records('scale', array('courseid'=>$courseid, 'scale'=>$data['scale']));
|
2007-09-15 21:17:11 +00:00
|
|
|
|
|
|
|
if (empty($old->id) or $old->courseid != $courseid) {
|
|
|
|
if ($count) {
|
|
|
|
$errors['scale'] = get_string('duplicatescale', 'grades');
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if ($old->scale != $data['scale']) {
|
|
|
|
if ($count) {
|
|
|
|
$errors['scale'] = get_string('duplicatescale', 'grades');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 10:49:41 +00:00
|
|
|
$options = explode(',', $data['scale']);
|
|
|
|
if (count($options) < 2) {
|
|
|
|
$errors['scale'] = get_string('badlyformattedscale', 'grades');
|
2007-09-15 21:17:11 +00:00
|
|
|
}
|
2007-07-29 23:02:03 +00:00
|
|
|
}
|
|
|
|
|
2007-11-23 22:15:07 +00:00
|
|
|
return $errors;
|
2007-07-29 23:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|