PageRenderTime 86ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/edit/scale/edit_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 164 lines | 91 code | 38 blank | 35 comment | 22 complexity | bfcd17f6c543937bf158e4168b0bd7a0 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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. /**
  17. * Edit form for grade scales
  18. *
  19. * @package core_grades
  20. * @copyright 2007 Petr Skoda
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. if (!defined('MOODLE_INTERNAL')) {
  24. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  25. }
  26. require_once $CFG->libdir.'/formslib.php';
  27. class edit_scale_form extends moodleform {
  28. function definition() {
  29. global $CFG;
  30. $mform =& $this->_form;
  31. // visible elements
  32. $mform->addElement('header', 'general', get_string('scale'));
  33. $mform->addElement('text', 'name', get_string('name'), 'size="40"');
  34. $mform->addRule('name', get_string('required'), 'required', null, 'client');
  35. $mform->setType('name', PARAM_TEXT);
  36. $mform->addElement('advcheckbox', 'standard', get_string('scalestandard'));
  37. $mform->addHelpButton('standard', 'scalestandard');
  38. $mform->addElement('static', 'used', get_string('used'));
  39. $mform->addElement('textarea', 'scale', get_string('scale'), array('cols'=>50, 'rows'=>2));
  40. $mform->addHelpButton('scale', 'scale');
  41. $mform->addRule('scale', get_string('required'), 'required', null, 'client');
  42. $mform->setType('scale', PARAM_TEXT);
  43. $mform->addElement('editor', 'description_editor', get_string('description'), null, $this->_customdata['editoroptions']);
  44. // hidden params
  45. $mform->addElement('hidden', 'id', 0);
  46. $mform->setType('id', PARAM_INT);
  47. $mform->addElement('hidden', 'courseid', 0);
  48. $mform->setType('courseid', PARAM_INT);
  49. /// add return tracking info
  50. $gpr = $this->_customdata['gpr'];
  51. $gpr->add_mform_elements($mform);
  52. //-------------------------------------------------------------------------------
  53. // buttons
  54. $this->add_action_buttons();
  55. }
  56. /// tweak the form - depending on existing data
  57. function definition_after_data() {
  58. global $CFG;
  59. $mform =& $this->_form;
  60. $courseid = $mform->getElementValue('courseid');
  61. if ($id = $mform->getElementValue('id')) {
  62. $scale = grade_scale::fetch(array('id'=>$id));
  63. $used = $scale->is_used();
  64. if ($used) {
  65. $mform->hardFreeze('scale');
  66. }
  67. if (empty($courseid)) {
  68. $mform->hardFreeze('standard');
  69. } else if (!has_capability('moodle/course:managescales', context_system::instance())) {
  70. //if they dont have managescales at system level the shouldnt be allowed to make scales standard (or not standard)
  71. $mform->hardFreeze('standard');
  72. } else if ($used and !empty($scale->courseid)) {
  73. $mform->hardFreeze('standard');
  74. }
  75. $usedstr = $scale->is_used() ? get_string('yes') : get_string('no');
  76. $used_el =& $mform->getElement('used');
  77. $used_el->setValue($usedstr);
  78. } else {
  79. $mform->removeElement('used');
  80. if (empty($courseid) or !has_capability('moodle/course:managescales', context_system::instance())) {
  81. $mform->hardFreeze('standard');
  82. }
  83. }
  84. }
  85. /// perform extra validation before submission
  86. function validation($data, $files) {
  87. global $CFG, $COURSE, $DB;
  88. $errors = parent::validation($data, $files);
  89. // we can not allow 2 scales with the same exact scale as this creates
  90. // problems for backup/restore
  91. $old = grade_scale::fetch(array('id'=>$data['id']));
  92. if (array_key_exists('standard', $data)) {
  93. if (empty($data['standard'])) {
  94. $courseid = $COURSE->id;
  95. } else {
  96. $courseid = 0;
  97. }
  98. } else {
  99. $courseid = $old->courseid;
  100. }
  101. if (array_key_exists('scale', $data)) {
  102. $scalearray = explode(',', $data['scale']);
  103. $scalearray = array_map('trim', $scalearray);
  104. $scaleoptioncount = count($scalearray);
  105. if (count($scalearray) < 2) {
  106. $errors['scale'] = get_string('badlyformattedscale', 'grades');
  107. } else {
  108. $thescale = implode(',',$scalearray);
  109. //this check strips out whitespace from the scale we're validating but not from those already in the DB
  110. $count = $DB->count_records_select('scale', "courseid=:courseid AND ".$DB->sql_compare_text('scale', core_text::strlen($thescale)).'=:scale',
  111. array('courseid'=>$courseid, 'scale'=>$thescale));
  112. if ($count) {
  113. //if this is a new scale but we found a duplice in the DB
  114. //or we found a duplicate in another course report the error
  115. if (empty($old->id) or $old->courseid != $courseid) {
  116. $errors['scale'] = get_string('duplicatescale', 'grades');
  117. } else if ($old->scale !== $thescale and $old->scale !== $data['scale']) {
  118. //if the old scale from DB is different but we found a duplicate then we're trying to modify a scale to be a duplicate
  119. $errors['scale'] = get_string('duplicatescale', 'grades');
  120. }
  121. }
  122. }
  123. }
  124. return $errors;
  125. }
  126. }