PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/moodle/grade/edit/outcome/edit_form.php

https://bitbucket.org/geek745/moodle-db2
PHP | 150 lines | 92 code | 35 blank | 23 comment | 17 complexity | f1b504c59e08524c08f824bddca9f659 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.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. require_once $CFG->libdir.'/formslib.php';
  17. class edit_outcome_form extends moodleform {
  18. function definition() {
  19. global $CFG, $COURSE;
  20. $mform =& $this->_form;
  21. // visible elements
  22. $mform->addElement('header', 'general', get_string('outcomes', 'grades'));
  23. $mform->addElement('text', 'fullname', get_string('fullname'), 'size="40"');
  24. $mform->addRule('fullname', get_string('required'), 'required');
  25. $mform->setType('fullname', PARAM_TEXT);
  26. $mform->addElement('text', 'shortname', get_string('shortname'), 'size="20"');
  27. $mform->addRule('shortname', get_string('required'), 'required');
  28. $mform->setType('shortname', PARAM_NOTAGS);
  29. $mform->addElement('advcheckbox', 'standard', get_string('outcomestandard', 'grades'));
  30. $mform->setHelpButton('standard', array('outcomestandard', get_string('outcomestandard'), 'grade'));
  31. $options = array();
  32. $mform->addElement('selectwithlink', 'scaleid', get_string('scale'), $options, null,
  33. array('link' => $CFG->wwwroot.'/grade/edit/scale/edit.php?courseid='.$COURSE->id, 'label' => get_string('scalescustomcreate')));
  34. $mform->setHelpButton('scaleid', array('scaleid', get_string('scale'), 'grade'));
  35. $mform->addRule('scaleid', get_string('required'), 'required');
  36. $mform->addElement('htmleditor', 'description', get_string('description'), array('cols'=>80, 'rows'=>20));
  37. // hidden params
  38. $mform->addElement('hidden', 'id', 0);
  39. $mform->setType('id', PARAM_INT);
  40. $mform->addElement('hidden', 'courseid', 0);
  41. $mform->setType('courseid', PARAM_INT);
  42. /// add return tracking info
  43. $gpr = $this->_customdata['gpr'];
  44. $gpr->add_mform_elements($mform);
  45. //-------------------------------------------------------------------------------
  46. // buttons
  47. $this->add_action_buttons();
  48. }
  49. /// tweak the form - depending on existing data
  50. function definition_after_data() {
  51. global $CFG;
  52. $mform =& $this->_form;
  53. // first load proper scales
  54. if ($courseid = $mform->getElementValue('courseid')) {
  55. $options = array();
  56. if ($scales = grade_scale::fetch_all_local($courseid)) {
  57. $options[-1] = '--'.get_string('scalescustom');
  58. foreach($scales as $scale) {
  59. $options[$scale->id] = $scale->get_name();
  60. }
  61. }
  62. if ($scales = grade_scale::fetch_all_global()) {
  63. $options[-2] = '--'.get_string('scalesstandard');
  64. foreach($scales as $scale) {
  65. $options[$scale->id] = $scale->get_name();
  66. }
  67. }
  68. $scale_el =& $mform->getElement('scaleid');
  69. $scale_el->load($options);
  70. } else {
  71. $options = array();
  72. if ($scales = grade_scale::fetch_all_global()) {
  73. foreach($scales as $scale) {
  74. $options[$scale->id] = $scale->get_name();
  75. }
  76. }
  77. $scale_el =& $mform->getElement('scaleid');
  78. $scale_el->load($options);
  79. }
  80. if ($id = $mform->getElementValue('id')) {
  81. $outcome = grade_outcome::fetch(array('id'=>$id));
  82. $itemcount = $outcome->get_item_uses_count();
  83. $coursecount = $outcome->get_course_uses_count();
  84. if ($itemcount) {
  85. $mform->hardFreeze('scaleid');
  86. }
  87. if (empty($courseid)) {
  88. $mform->hardFreeze('standard');
  89. } else if (empty($outcome->courseid) and !has_capability('moodle/grade:manage', get_context_instance(CONTEXT_SYSTEM))) {
  90. $mform->hardFreeze('standard');
  91. } else if ($coursecount and empty($outcome->courseid)) {
  92. $mform->hardFreeze('standard');
  93. }
  94. } else {
  95. if (empty($courseid) or !has_capability('moodle/grade:manage', get_context_instance(CONTEXT_SYSTEM))) {
  96. $mform->hardFreeze('standard');
  97. }
  98. }
  99. }
  100. /// perform extra validation before submission
  101. function validation($data, $files) {
  102. $errors = parent::validation($data, $files);
  103. if ($data['scaleid'] < 1) {
  104. $errors['scaleid'] = get_string('required');
  105. }
  106. if (!empty($data['standard']) and $scale = grade_scale::fetch(array('id'=>$data['scaleid']))) {
  107. if (!empty($scale->courseid)) {
  108. //TODO: localize
  109. $errors['scaleid'] = 'Can not use custom scale in global outcome!';
  110. }
  111. }
  112. return $errors;
  113. }
  114. }
  115. ?>