CoursesListRow.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 CreateUsersView from 'compiled/views/courses/roster/CreateUsersView'
  19. import RosterUserCollection from 'compiled/collections/RosterUserCollection'
  20. import SectionCollection from 'compiled/collections/SectionCollection'
  21. import RolesCollection from 'compiled/collections/RolesCollection'
  22. import Role from 'compiled/models/Role'
  23. import CreateUserList from 'compiled/models/CreateUserList'
  24. import $ from 'jquery'
  25. import React from 'react'
  26. import PropTypes from 'prop-types'
  27. import {Model} from 'Backbone'
  28. import I18n from 'i18n!account_course_user_search'
  29. import _ from 'underscore'
  30. import UserLink from './UserLink'
  31. const { number, string, shape, arrayOf } = PropTypes;
  32. class CoursesListRow extends React.Component {
  33. static propTypes = {
  34. id: string.isRequired,
  35. name: string.isRequired,
  36. workflow_state: string.isRequired,
  37. total_students: number.isRequired,
  38. teachers: arrayOf(shape(UserLink.propTypes)).isRequired,
  39. sis_course_id: string.isRequired,
  40. subaccount_name: string.isRequired,
  41. term: shape({name: string.isRequired}).isRequired,
  42. urls: shape({ ENROLL_USERS_URL: string.isRequired, USER_LISTS_URL: string.isRequired }),
  43. roles: arrayOf(shape({ id: string.isRequired })),
  44. sections: arrayOf(shape(UserLink.propTypes)),
  45. };
  46. static defaultProps = {
  47. roles: [],
  48. urls: {ENROLL_USERS_URL: '', USER_LISTS_URL: ''},
  49. sections: [],
  50. };
  51. constructor (props) {
  52. super(props)
  53. const teachers = _.uniq(props.teachers, t => t.id);
  54. this.state = {
  55. teachersToShow: _.compact([teachers[0], teachers[1]])
  56. }
  57. }
  58. showMoreLink = () => {
  59. if (this.props.teachers.length > 2 && this.state.teachersToShow.length === 2) {
  60. return <a className="showMoreLink" href="#" onClick={this.showMoreTeachers}> {I18n.t('Show More')}</a>
  61. }
  62. }
  63. showMoreTeachers = () => {
  64. this.setState({teachersToShow: _.uniq(this.props.teachers, t => t.id)});
  65. }
  66. addUserToCourse = () => {
  67. const course = new Model({id: this.props.id});
  68. const userCollection = new RosterUserCollection(null, {
  69. course_id: this.props.id,
  70. sections: new SectionCollection(this.props.sections),
  71. params: {
  72. include: ['avatar_url', 'enrollments', 'email', 'observed_users', 'can_be_removed'],
  73. per_page: 50
  74. }
  75. });
  76. userCollection.fetch();
  77. userCollection.once('reset', () => {
  78. userCollection.on('reset', () => {
  79. const numUsers = userCollection.length;
  80. let msg = '';
  81. if (numUsers === 0) {
  82. msg = I18n.t('No matching users found.');
  83. } else if (numUsers === 1) {
  84. msg = I18n.t('1 user found.');
  85. } else {
  86. msg = I18n.t('%{userCount} users found.', {userCount: numUsers});
  87. }
  88. $('#aria_alerts').empty().text(msg);
  89. });
  90. });
  91. const createUsersViewParams = {
  92. collection: userCollection,
  93. rolesCollection: new RolesCollection(this.props.roles.map(role => new Role(role))),
  94. model: new CreateUserList({
  95. sections: this.props.sections,
  96. roles: this.props.roles,
  97. readURL: this.props.urls.USER_LISTS_URL,
  98. updateURL: this.props.urls.ENROLL_USERS_URL
  99. }),
  100. courseModel: course,
  101. title: I18n.t('Add People'),
  102. height: 520,
  103. className: 'form-dialog'
  104. };
  105. const createUsersBackboneView = new CreateUsersView(createUsersViewParams);
  106. createUsersBackboneView.open();
  107. createUsersBackboneView.on('close', () => {
  108. createUsersBackboneView.remove()
  109. });
  110. }
  111. render () {
  112. const { id, name, workflow_state, sis_course_id, total_students, subaccount_name } = this.props;
  113. const url = `/courses/${id}`;
  114. const isPublished = workflow_state !== 'unpublished';
  115. return (
  116. <div role="row" className="grid-row pad-box-mini border border-b">
  117. <div className="col-xs-3">
  118. <div role="gridcell" className="grid-row middle-xs">
  119. <div className="col-xs-2">{isPublished && (<i className="icon-publish icon-Solid courses-list__published-icon" />)}</div>
  120. <div className="col-xs-10">
  121. <div className="courseName">
  122. <a href={url}>{name}</a>
  123. </div>
  124. </div>
  125. </div>
  126. </div>
  127. <div className="col-xs-1" role="gridcell">
  128. <div className="courseSIS">{sis_course_id}</div>
  129. </div>
  130. <div className="col-xs-1" role="gridcell">
  131. <div className="courseSIS">{this.props.term.name}</div>
  132. </div>
  133. <div className="col-xs-2" role="gridcell">
  134. {this.state.teachersToShow && this.state.teachersToShow.map(teacher => <UserLink key={teacher.id} {...teacher} />)}
  135. { this.showMoreLink() }
  136. </div>
  137. <div className="col-xs-2" role="gridcell">
  138. <div className="courseSubaccount">{subaccount_name}</div>
  139. </div>
  140. <div className="col-xs-1" role="gridcell">
  141. <div className="totalStudents">{I18n.n(total_students)}</div>
  142. </div>
  143. <div className="col-xs-2" role="gridcell">
  144. <div className="courses-user-list-actions">
  145. <button className="Button Button--icon-action addUserButton" onClick={this.addUserToCourse} type="button">
  146. <span className="screenreader-only">{I18n.t('Add Users to %{name}', {name: this.props.name})}</span>
  147. <i className="icon-plus" aria-hidden="true" />
  148. </button>
  149. <a className="Button Button--icon-action" href={`/courses/${this.props.id}/statistics`}>
  150. <span className="screenreader-only">{I18n.t('Go to statistics for %{name}', {name: this.props.name})}</span>
  151. <i className="icon-stats" aria-hidden="true" />
  152. </a>
  153. <a className="Button Button--icon-action" href={`/courses/${this.props.id}/settings`}>
  154. <span className="screenreader-only">{I18n.t('Go to settings for %{name}', {name: this.props.name})}</span>
  155. <i className="icon-settings" aria-hidden="true" />
  156. </a>
  157. </div>
  158. </div>
  159. </div>
  160. );
  161. }
  162. }
  163. export default CoursesListRow