/moodle/grade/edit/scale/edit_form.php

https://bitbucket.org/geek745/moodle-db2 · PHP · 147 lines · 86 code · 38 blank · 23 comment · 22 complexity · 01cf82116ad8778a7d0de255a219329e MD5 · raw file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. require_once $CFG->libdir.'/formslib.php';
  17. class edit_scale_form extends moodleform {
  18. function definition() {
  19. global $CFG;
  20. $mform =& $this->_form;
  21. // visible elements
  22. $mform->addElement('header', 'general', get_string('scale'));
  23. $mform->addElement('text', 'name', get_string('name'), 'size="40"');
  24. $mform->addRule('name', get_string('required'), 'required', null, 'client');
  25. $mform->setType('name', PARAM_TEXT);
  26. $mform->addElement('advcheckbox', 'standard', get_string('scalestandard'));
  27. $mform->setHelpButton('standard', array('scalestandard', get_string('scalestandard'), 'grade'));
  28. $mform->addElement('static', 'used', get_string('used'));
  29. $mform->addElement('textarea', 'scale', get_string('scale'), array('cols'=>50, 'rows'=>2));
  30. $mform->setHelpButton('scale', array('scales', get_string('scale')));
  31. $mform->addRule('scale', get_string('required'), 'required', null, 'client');
  32. $mform->setType('scale', PARAM_TEXT);
  33. $mform->addElement('htmleditor', 'description', get_string('description'), array('cols'=>80, 'rows'=>20));
  34. // hidden params
  35. $mform->addElement('hidden', 'id', 0);
  36. $mform->setType('id', PARAM_INT);
  37. $mform->addElement('hidden', 'courseid', 0);
  38. $mform->setType('courseid', PARAM_INT);
  39. /// add return tracking info
  40. $gpr = $this->_customdata['gpr'];
  41. $gpr->add_mform_elements($mform);
  42. //-------------------------------------------------------------------------------
  43. // buttons
  44. $this->add_action_buttons();
  45. }
  46. /// tweak the form - depending on existing data
  47. function definition_after_data() {
  48. global $CFG;
  49. $mform =& $this->_form;
  50. $courseid = $mform->getElementValue('courseid');
  51. if ($id = $mform->getElementValue('id')) {
  52. $scale = grade_scale::fetch(array('id'=>$id));
  53. $used = $scale->is_used();
  54. if ($used) {
  55. $mform->hardFreeze('scale');
  56. }
  57. if (empty($courseid)) {
  58. $mform->hardFreeze('standard');
  59. } else if (empty($scale->courseid) and !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) {
  60. $mform->hardFreeze('standard');
  61. } else if ($used and !empty($scale->courseid)) {
  62. $mform->hardFreeze('standard');
  63. }
  64. $usedstr = $scale->is_used() ? get_string('yes') : get_string('no');
  65. $used_el =& $mform->getElement('used');
  66. $used_el->setValue($usedstr);
  67. } else {
  68. $mform->removeElement('used');
  69. if (empty($courseid) or !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) {
  70. $mform->hardFreeze('standard');
  71. }
  72. }
  73. }
  74. /// perform extra validation before submission
  75. function validation($data, $files) {
  76. global $CFG, $COURSE;
  77. $errors = parent::validation($data, $files);
  78. // we can not allow 2 scales with the same exact scale as this creates
  79. // problems for backup/restore
  80. $old = grade_scale::fetch(array('id'=>$data['id']));
  81. if (array_key_exists('standard', $data)) {
  82. if (empty($data['standard'])) {
  83. $courseid = $COURSE->id;
  84. } else {
  85. $courseid = 0;
  86. }
  87. } else {
  88. $courseid = $old->courseid;
  89. }
  90. if (array_key_exists('scale', $data)) {
  91. $count = count_records('scale', 'courseid', $courseid, 'scale', $data['scale']);
  92. if (empty($old->id) or $old->courseid != $courseid) {
  93. if ($count) {
  94. $errors['scale'] = get_string('duplicatescale', 'grades');
  95. }
  96. } else if ($old->scale != $data['scale']) {
  97. if ($count) {
  98. $errors['scale'] = get_string('duplicatescale', 'grades');
  99. }
  100. }
  101. $options = explode(',', $data['scale']);
  102. if (count($options) < 2) {
  103. $errors['scale'] = get_string('badlyformattedscale', 'grades');
  104. }
  105. }
  106. return $errors;
  107. }
  108. }
  109. ?>