PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/import/grade_import_form.php

http://github.com/moodle/moodle
PHP | 172 lines | 127 code | 23 blank | 22 comment | 10 complexity | 74dbcd13be1f4261e2f35c4aa9679952 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. if (!defined('MOODLE_INTERNAL')) {
  17. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  18. }
  19. require_once $CFG->libdir.'/formslib.php';
  20. require_once($CFG->libdir.'/gradelib.php');
  21. class grade_import_form extends moodleform {
  22. function definition (){
  23. global $COURSE;
  24. $mform =& $this->_form;
  25. if (isset($this->_customdata)) { // hardcoding plugin names here is hacky
  26. $features = $this->_customdata;
  27. } else {
  28. $features = array();
  29. }
  30. // course id needs to be passed for auth purposes
  31. $mform->addElement('hidden', 'id', optional_param('id', 0, PARAM_INT));
  32. $mform->setType('id', PARAM_INT);
  33. $mform->addElement('header', 'general', get_string('importfile', 'grades'));
  34. // Restrict the possible upload file types.
  35. if (!empty($features['acceptedtypes'])) {
  36. $acceptedtypes = $features['acceptedtypes'];
  37. } else {
  38. $acceptedtypes = '*';
  39. }
  40. // File upload.
  41. $mform->addElement('filepicker', 'userfile', get_string('file'), null, array('accepted_types' => $acceptedtypes));
  42. $mform->addRule('userfile', null, 'required');
  43. $encodings = core_text::get_encodings();
  44. $mform->addElement('select', 'encoding', get_string('encoding', 'grades'), $encodings);
  45. $mform->addHelpButton('encoding', 'encoding', 'grades');
  46. if (!empty($features['includeseparator'])) {
  47. $radio = array();
  48. $radio[] = $mform->createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab');
  49. $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma');
  50. $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcolon', 'grades'), 'colon');
  51. $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepsemicolon', 'grades'), 'semicolon');
  52. $mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false);
  53. $mform->addHelpButton('separator', 'separator', 'grades');
  54. $mform->setDefault('separator', 'comma');
  55. }
  56. if (!empty($features['verbosescales'])) {
  57. $options = array(1=>get_string('yes'), 0=>get_string('no'));
  58. $mform->addElement('select', 'verbosescales', get_string('verbosescales', 'grades'), $options);
  59. $mform->addHelpButton('verbosescales', 'verbosescales', 'grades');
  60. }
  61. $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
  62. $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'grades'), $options); // TODO: localize
  63. $mform->addHelpButton('previewrows', 'rowpreviewnum', 'grades');
  64. $mform->setType('previewrows', PARAM_INT);
  65. $mform->addElement('checkbox', 'forceimport', get_string('forceimport', 'grades'));
  66. $mform->addHelpButton('forceimport', 'forceimport', 'grades');
  67. $mform->setDefault('forceimport', false);
  68. $mform->setType('forceimport', PARAM_BOOL);
  69. $mform->addElement('hidden', 'groupid', groups_get_course_group($COURSE));
  70. $mform->setType('groupid', PARAM_INT);
  71. $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
  72. }
  73. }
  74. class grade_import_mapping_form extends moodleform {
  75. function definition () {
  76. global $CFG, $COURSE;
  77. $mform =& $this->_form;
  78. // this is an array of headers
  79. $header = $this->_customdata['header'];
  80. // course id
  81. $mform->addElement('header', 'general', get_string('identifier', 'grades'));
  82. $mapfromoptions = array();
  83. if ($header) {
  84. foreach ($header as $i=>$h) {
  85. $mapfromoptions[$i] = s($h);
  86. }
  87. }
  88. $mform->addElement('select', 'mapfrom', get_string('mapfrom', 'grades'), $mapfromoptions);
  89. $mform->addHelpButton('mapfrom', 'mapfrom', 'grades');
  90. $maptooptions = array(
  91. 'userid' => get_string('userid', 'grades'),
  92. 'username' => get_string('username'),
  93. 'useridnumber' => get_string('idnumber'),
  94. 'useremail' => get_string('email'),
  95. '0' => get_string('ignore', 'grades')
  96. );
  97. $mform->addElement('select', 'mapto', get_string('mapto', 'grades'), $maptooptions);
  98. $mform->addHelpButton('mapto', 'mapto', 'grades');
  99. $mform->addElement('header', 'general_map', get_string('mappings', 'grades'));
  100. $mform->addHelpButton('general_map', 'mappings', 'grades');
  101. // Add a feedback option.
  102. $feedbacks = array();
  103. if ($gradeitems = $this->_customdata['gradeitems']) {
  104. foreach ($gradeitems as $itemid => $itemname) {
  105. $feedbacks['feedback_'.$itemid] = get_string('feedbackforgradeitems', 'grades', $itemname);
  106. }
  107. }
  108. if ($header) {
  109. $i = 0; // index
  110. foreach ($header as $h) {
  111. $h = trim($h);
  112. // This is what each header maps to.
  113. $headermapsto = array(
  114. get_string('others', 'grades') => array(
  115. '0' => get_string('ignore', 'grades'),
  116. 'new' => get_string('newitem', 'grades')
  117. ),
  118. get_string('gradeitems', 'grades') => $gradeitems,
  119. get_string('feedbacks', 'grades') => $feedbacks
  120. );
  121. $mform->addElement('selectgroups', 'mapping_'.$i, s($h), $headermapsto);
  122. $i++;
  123. }
  124. }
  125. // course id needs to be passed for auth purposes
  126. $mform->addElement('hidden', 'map', 1);
  127. $mform->setType('map', PARAM_INT);
  128. $mform->setConstant('map', 1);
  129. $mform->addElement('hidden', 'id', $this->_customdata['id']);
  130. $mform->setType('id', PARAM_INT);
  131. $mform->setConstant('id', $this->_customdata['id']);
  132. $mform->addElement('hidden', 'iid', $this->_customdata['iid']);
  133. $mform->setType('iid', PARAM_INT);
  134. $mform->setConstant('iid', $this->_customdata['iid']);
  135. $mform->addElement('hidden', 'importcode', $this->_customdata['importcode']);
  136. $mform->setType('importcode', PARAM_FILE);
  137. $mform->setConstant('importcode', $this->_customdata['importcode']);
  138. $mform->addElement('hidden', 'verbosescales', 1);
  139. $mform->setType('verbosescales', PARAM_INT);
  140. $mform->setConstant('verbosescales', $this->_customdata['verbosescales']);
  141. $mform->addElement('hidden', 'groupid', groups_get_course_group($COURSE));
  142. $mform->setType('groupid', PARAM_INT);
  143. $mform->setConstant('groupid', groups_get_course_group($COURSE));
  144. $mform->addElement('hidden', 'forceimport', $this->_customdata['forceimport']);
  145. $mform->setType('forceimport', PARAM_BOOL);
  146. $mform->setConstant('forceimport', $this->_customdata['forceimport']);
  147. $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
  148. }
  149. }