gradingStandardCollection.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2015 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import React from 'react'
  19. import update from 'immutability-helper'
  20. import GradingStandard from 'jsx/grading/gradingStandard'
  21. import $ from 'jquery'
  22. import I18n from 'i18n!external_tools'
  23. import _ from 'underscore'
  24. import 'jquery.instructure_misc_plugins'
  25. var GradingStandardCollection = React.createClass({
  26. getInitialState: function() {
  27. return {standards: null};
  28. },
  29. componentWillMount: function() {
  30. $.getJSON(ENV.GRADING_STANDARDS_URL + ".json")
  31. .done(this.gotStandards)
  32. },
  33. gotStandards: function(standards) {
  34. var formattedStandards = $.extend(true, [], standards);
  35. formattedStandards = _.map(formattedStandards, function(standard) {
  36. standard.grading_standard.data = this.formatStandardData(standard.grading_standard.data);
  37. return standard;
  38. }, this);
  39. this.setState({standards: formattedStandards});
  40. },
  41. formatStandardData: function(standardData) {
  42. return _.map(standardData, function(dataRow){
  43. return [dataRow[0], this.roundToTwoDecimalPlaces(dataRow[1] * 100)];
  44. }, this);
  45. },
  46. addGradingStandard: function() {
  47. var newStandard = {
  48. editing: true,
  49. justAdded: true,
  50. grading_standard: {
  51. permissions: { manage: true },
  52. title: "",
  53. data: this.formatStandardData(ENV.DEFAULT_GRADING_STANDARD_DATA),
  54. id: -1
  55. }
  56. };
  57. var newStandards = update(this.state.standards, {$unshift: [newStandard]});
  58. this.setState({standards: newStandards});
  59. },
  60. getStandardById: function(id) {
  61. return _.find(this.state.standards, function(standard){ return standard.grading_standard.id === id });
  62. },
  63. standardNotCreated: function(gradingStandard){
  64. return gradingStandard.id === -1;
  65. },
  66. setEditingStatus: function(id, setEditingStatusTo){
  67. var newStandards = $.extend(true, [], this.state.standards);
  68. var existingStandard = this.getStandardById(id);
  69. var indexToEdit = this.state.standards.indexOf(existingStandard);
  70. if(setEditingStatusTo === false && this.standardNotCreated(existingStandard.grading_standard)){
  71. newStandards.splice(indexToEdit, 1);
  72. this.setState({standards: newStandards});
  73. }else{
  74. newStandards[indexToEdit].editing = setEditingStatusTo;
  75. this.setState({standards: newStandards});
  76. }
  77. },
  78. anyStandardBeingEdited: function() {
  79. return !!_.find(this.state.standards, function(standard){return standard.editing});
  80. },
  81. saveGradingStandard: function(standard) {
  82. var newStandards = $.extend(true, [], this.state.standards);
  83. var indexToUpdate = this.state.standards.indexOf(this.getStandardById(standard.id));
  84. var type, url, data;
  85. standard.title = standard.title.trim();
  86. if(this.standardNotCreated(standard)){
  87. if(standard.title === "") standard.title = "New Grading Scheme";
  88. type = "POST";
  89. url = ENV.GRADING_STANDARDS_URL;
  90. data = this.dataFormattedForCreate(standard);
  91. }else{
  92. type = "PUT";
  93. url = ENV.GRADING_STANDARDS_URL + "/" + standard.id;
  94. data = this.dataFormattedForUpdate(standard);
  95. }
  96. $.ajax({
  97. type: type,
  98. url: url,
  99. dataType: "json",
  100. contentType: 'application/json',
  101. data: JSON.stringify(data),
  102. context: this
  103. })
  104. .success(function(updatedStandard){
  105. updatedStandard.grading_standard.data = this.formatStandardData(updatedStandard.grading_standard.data);
  106. newStandards[indexToUpdate] = updatedStandard;
  107. this.setState({standards: newStandards}, function() {
  108. $.flashMessage(I18n.t("Grading scheme saved"));
  109. });
  110. })
  111. .error(function(){
  112. newStandards[indexToUpdate].grading_standard.saving = false;
  113. this.setState({standards: newStandards}, function() {
  114. $.flashError(I18n.t("There was a problem saving the grading scheme"));
  115. });
  116. });
  117. },
  118. dataFormattedForCreate: function(standard) {
  119. var formattedData = { grading_standard: standard };
  120. _.each(standard.data, function(dataRow, i){
  121. var name = dataRow[0];
  122. var value = dataRow[1];
  123. formattedData["grading_standard"]["data"][i] = [
  124. name.trim(),
  125. this.roundToTwoDecimalPlaces(value) / 100
  126. ];
  127. }, this);
  128. return formattedData;
  129. },
  130. dataFormattedForUpdate: function(standard) {
  131. var formattedData = { grading_standard: { title: standard.title, standard_data: {} } };
  132. _.each(standard.data, function(dataRow, i){
  133. var name = dataRow[0];
  134. var value = dataRow[1];
  135. formattedData["grading_standard"]["standard_data"]["scheme_" + i] = {
  136. name: name.trim(),
  137. value: this.roundToTwoDecimalPlaces(value)
  138. };
  139. }, this);
  140. return formattedData;
  141. },
  142. roundToTwoDecimalPlaces: function(number) {
  143. return Math.round(number * 100)/100;
  144. },
  145. deleteGradingStandard: function(event, uniqueId) {
  146. var self = this,
  147. $standard = $(event.target).parents(".grading_standard");
  148. $standard.confirmDelete({
  149. url: ENV.GRADING_STANDARDS_URL + "/" + uniqueId,
  150. message: I18n.t("Are you sure you want to delete this grading scheme?"),
  151. success: function() {
  152. var indexToRemove = self.state.standards.indexOf(self.getStandardById(uniqueId));
  153. var newStandards = update(self.state.standards, {$splice: [[indexToRemove, 1]]});
  154. self.setState({standards: newStandards}, function(){
  155. $.flashMessage(I18n.t("Grading scheme deleted"))
  156. });
  157. },
  158. error: function() {
  159. $.flashError(I18n.t("There was a problem deleting the grading scheme"));
  160. }
  161. });
  162. },
  163. hasAdminOrTeacherRole: function() {
  164. return _.intersection(ENV.current_user_roles, ["teacher", "admin"]).length > 0;
  165. },
  166. getAddButtonCssClasses: function() {
  167. var classes = "Button pull-right add_standard_button"
  168. if(!this.hasAdminOrTeacherRole() || this.anyStandardBeingEdited()) classes += " disabled"
  169. return classes;
  170. },
  171. renderGradingStandards: function() {
  172. if(!this.state.standards){
  173. return null;
  174. } else if(this.state.standards.length === 0){
  175. return <h3 ref="noSchemesMessage">{I18n.t("No grading schemes to display")}</h3>;
  176. }
  177. return this.state.standards.map(function(s){
  178. return (<GradingStandard ref={"gradingStandard" + s.grading_standard.id} key={s.grading_standard.id}
  179. uniqueId={s.grading_standard.id} standard={s.grading_standard}
  180. editing={!!s.editing} permissions={s.grading_standard.permissions}
  181. justAdded={!!s.justAdded} onSetEditingStatus={this.setEditingStatus}
  182. round={this.roundToTwoDecimalPlaces} onDeleteGradingStandard={this.deleteGradingStandard}
  183. othersEditing={!s.editing && this.anyStandardBeingEdited()}
  184. onSaveGradingStandard={this.saveGradingStandard}/>);
  185. }, this);
  186. },
  187. render: function () {
  188. return(
  189. <div>
  190. <div className="pull-right">
  191. <button ref="addButton" onClick={this.addGradingStandard} className={this.getAddButtonCssClasses()}>
  192. <i className="icon-add"/>
  193. {I18n.t(" Add grading scheme")}
  194. </button>
  195. </div>
  196. <div id="standards" className="content-box react_grading_standards">
  197. {this.renderGradingStandards()}
  198. </div>
  199. </div>
  200. );
  201. }
  202. });
  203. export default GradingStandardCollection