PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/edit/tree/calculation_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 107 lines | 56 code | 23 blank | 28 comment | 7 complexity | f1253be7ed0b37647dd499c09f60ce22 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. * A moodleform to allow the editing of a calculated grade item
  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_calculation_form extends moodleform {
  28. public $available;
  29. public $noidnumbers;
  30. function definition() {
  31. global $COURSE;
  32. $mform =& $this->_form;
  33. $itemid = $this->_customdata['itemid'];
  34. $this->available = grade_item::fetch_all(array('courseid'=>$COURSE->id));
  35. $this->noidnumbers = array();
  36. // All items that have no idnumbers are added to a separate section of the form (hidden by default),
  37. // enabling the user to assign idnumbers to these grade_items.
  38. foreach ($this->available as $item) {
  39. if (empty($item->idnumber)) {
  40. $this->noidnumbers[$item->id] = $item;
  41. unset($this->available[$item->id]);
  42. }
  43. if ($item->id == $itemid) { // Do not include the current grade_item in the available section
  44. unset($this->available[$item->id]);
  45. }
  46. }
  47. /// visible elements
  48. $mform->addElement('header', 'general', get_string('gradeitem', 'grades'));
  49. $mform->addElement('static', 'itemname', get_string('itemname', 'grades'));
  50. $mform->addElement('textarea', 'calculation', get_string('calculation', 'grades'), 'cols="60" rows="5"');
  51. $mform->addHelpButton('calculation', 'calculation', 'grades');
  52. /// hidden params
  53. $mform->addElement('hidden', 'id', 0);
  54. $mform->setType('id', PARAM_INT);
  55. $mform->addElement('hidden', 'courseid', 0);
  56. $mform->setType('courseid', PARAM_INT);
  57. $mform->addElement('hidden', 'section', 0);
  58. $mform->setType('section', PARAM_ALPHA);
  59. $mform->setDefault('section', 'calculation');
  60. /// add return tracking info
  61. $gpr = $this->_customdata['gpr'];
  62. $gpr->add_mform_elements($mform);
  63. $this->add_action_buttons();
  64. }
  65. function definition_after_data() {
  66. global $CFG, $COURSE;
  67. $mform =& $this->_form;
  68. }
  69. /// perform extra validation before submission
  70. function validation($data, $files) {
  71. $errors = parent::validation($data, $files);
  72. $mform =& $this->_form;
  73. // check the calculation formula
  74. if ($data['calculation'] != '') {
  75. $grade_item = grade_item::fetch(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
  76. $calculation = calc_formula::unlocalize(stripslashes($data['calculation']));
  77. $result = $grade_item->validate_formula($calculation);
  78. if ($result !== true) {
  79. $errors['calculation'] = $result;
  80. }
  81. }
  82. return $errors;
  83. }
  84. }