[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/course/yui/src/management/js/ -> console.js (source)

   1  /* global DragDrop, Category, Course */
   2  
   3  /**
   4   * Provides drop down menus for list of action links.
   5   *
   6   * @module moodle-course-management
   7   */
   8  
   9  /**
  10   * Management JS console.
  11   *
  12   * Provides the organisation for course and category management JS.
  13   *
  14   * @namespace M.course.management
  15   * @class Console
  16   * @constructor
  17   * @extends Base
  18   */
  19  function Console() {
  20      Console.superclass.constructor.apply(this, arguments);
  21  }
  22  Console.NAME = 'moodle-course-management';
  23  Console.CSS_PREFIX = 'management';
  24  Console.ATTRS = {
  25      /**
  26       * The HTML element containing the management interface.
  27       * @attribute element
  28       * @type Node
  29       */
  30      element: {
  31          setter: function(node) {
  32              if (typeof node === 'string') {
  33                  node = Y.one('#' + node);
  34              }
  35              return node;
  36          }
  37      },
  38  
  39      /**
  40       * The category listing container node.
  41       * @attribute categorylisting
  42       * @type Node
  43       * @default null
  44       */
  45      categorylisting: {
  46          value: null
  47      },
  48  
  49      /**
  50       * The course listing container node.
  51       * @attribute courselisting
  52       * @type Node
  53       * @default null
  54       */
  55      courselisting: {
  56          value: null
  57      },
  58  
  59      /**
  60       * The course details container node.
  61       * @attribute coursedetails
  62       * @type Node|null
  63       * @default null
  64       */
  65      coursedetails: {
  66          value: null
  67      },
  68  
  69      /**
  70       * The id of the currently active category.
  71       * @attribute activecategoryid
  72       * @type Number
  73       * @default null
  74       */
  75      activecategoryid: {
  76          value: null
  77      },
  78  
  79      /**
  80       * The id of the currently active course.
  81       * @attribute activecourseid
  82       * @type Number
  83       * @default Null
  84       */
  85      activecourseid: {
  86          value: null
  87      },
  88  
  89      /**
  90       * The categories that are currently available through the management interface.
  91       * @attribute categories
  92       * @type Array
  93       * @default []
  94       */
  95      categories: {
  96          setter: function(item, name) {
  97              if (Y.Lang.isArray(item)) {
  98                  return item;
  99              }
 100              var items = this.get(name);
 101              items.push(item);
 102              return items;
 103          },
 104          value: []
 105      },
 106  
 107      /**
 108       * The courses that are currently available through the management interface.
 109       * @attribute courses
 110       * @type Course[]
 111       * @default Array
 112       */
 113      courses: {
 114          validator: function(val) {
 115              return Y.Lang.isArray(val);
 116          },
 117          value: []
 118      },
 119  
 120      /**
 121       * The currently displayed page of courses.
 122       * @attribute page
 123       * @type Number
 124       * @default null
 125       */
 126      page: {
 127          getter: function(value, name) {
 128              if (value === null) {
 129                  value = this.get('element').getData(name);
 130                  this.set(name, value);
 131              }
 132              return value;
 133          },
 134          value: null
 135      },
 136  
 137      /**
 138       * The total pages of courses that can be shown for this category.
 139       * @attribute totalpages
 140       * @type Number
 141       * @default null
 142       */
 143      totalpages: {
 144          getter: function(value, name) {
 145              if (value === null) {
 146                  value = this.get('element').getData(name);
 147                  this.set(name, value);
 148              }
 149              return value;
 150          },
 151          value: null
 152      },
 153  
 154      /**
 155       * The total number of courses belonging to this category.
 156       * @attribute totalcourses
 157       * @type Number
 158       * @default null
 159       */
 160      totalcourses: {
 161          getter: function(value, name) {
 162              if (value === null) {
 163                  value = this.get('element').getData(name);
 164                  this.set(name, value);
 165              }
 166              return value;
 167          },
 168          value: null
 169      },
 170  
 171      /**
 172       * The URL to use for AJAX actions/requests.
 173       * @attribute ajaxurl
 174       * @type String
 175       * @default /course/ajax/management.php
 176       */
 177      ajaxurl: {
 178          getter: function(value) {
 179              if (value === null) {
 180                  value = M.cfg.wwwroot + '/course/ajax/management.php';
 181              }
 182              return value;
 183          },
 184          value: null
 185      },
 186  
 187      /**
 188       * The drag drop handler
 189       * @attribute dragdrop
 190       * @type DragDrop
 191       * @default null
 192       */
 193      dragdrop: {
 194          value: null
 195      }
 196  };
 197  Console.prototype = {
 198  
 199      /**
 200       * Gets set to true once the first categories have been initialised.
 201       * @property categoriesinit
 202       * @private
 203       * @type {boolean}
 204       */
 205      categoriesinit: false,
 206  
 207      /**
 208       * Initialises a new instance of the Console.
 209       * @method initializer
 210       */
 211      initializer: function() {
 212          Y.log('Initialising course category management console', 'info', 'moodle-course-management');
 213          this.set('element', 'coursecat-management');
 214          var element = this.get('element'),
 215              categorylisting = element.one('#category-listing'),
 216              courselisting = element.one('#course-listing'),
 217              selectedcategory = null,
 218              selectedcourse = null;
 219  
 220          if (categorylisting) {
 221              selectedcategory = categorylisting.one('.listitem[data-selected="1"]');
 222          }
 223          if (courselisting) {
 224              selectedcourse = courselisting.one('.listitem[data-selected="1"]');
 225          }
 226          this.set('categorylisting', categorylisting);
 227          this.set('courselisting', courselisting);
 228          this.set('coursedetails', element.one('#course-detail'));
 229          if (selectedcategory) {
 230              this.set('activecategoryid', selectedcategory.getData('id'));
 231          }
 232          if (selectedcourse) {
 233              this.set('activecourseid', selectedcourse.getData('id'));
 234          }
 235          this.initialiseCategories(categorylisting);
 236          this.initialiseCourses();
 237  
 238          if (courselisting) {
 239              // No need for dragdrop if we don't have a course listing.
 240              this.set('dragdrop', new DragDrop({console: this}));
 241          }
 242      },
 243  
 244      /**
 245       * Initialises all the categories being shown.
 246       * @method initialiseCategories
 247       * @private
 248       * @return {boolean}
 249       */
 250      initialiseCategories: function(listing) {
 251          var count = 0;
 252          if (!listing) {
 253              return false;
 254          }
 255  
 256          // Disable category bulk actions as nothing will be selected on initialise.
 257          var menumovecatto = listing.one('#menumovecategoriesto');
 258          if (menumovecatto) {
 259              menumovecatto.setAttribute('disabled', true);
 260          }
 261          var menuresortcategoriesby = listing.one('#menuresortcategoriesby');
 262          if (menuresortcategoriesby) {
 263              menuresortcategoriesby.setAttribute('disabled', true);
 264          }
 265          var menuresortcoursesby = listing.one('#menuresortcoursesby');
 266          if (menuresortcoursesby) {
 267              menuresortcoursesby.setAttribute('disabled', true);
 268          }
 269  
 270          listing.all('.listitem[data-id]').each(function(node) {
 271              this.set('categories', new Category({
 272                  node: node,
 273                  console: this
 274              }));
 275              count++;
 276          }, this);
 277          if (!this.categoriesinit) {
 278              this.get('categorylisting').delegate('click', this.handleCategoryDelegation, 'a[data-action]', this);
 279              this.get('categorylisting').delegate('click', this.handleCategoryDelegation, 'input[name="bcat[]"]', this);
 280              this.get('categorylisting').delegate('click', this.handleBulkSortByaction, '#menuselectsortby', this);
 281              this.categoriesinit = true;
 282              Y.log(count + ' categories being managed', 'info', 'moodle-course-management');
 283          } else {
 284              Y.log(count + ' new categories being managed', 'info', 'moodle-course-management');
 285          }
 286      },
 287  
 288      /**
 289       * Initialises all the categories being shown.
 290       * @method initialiseCourses
 291       * @private
 292       * @return {boolean}
 293       */
 294      initialiseCourses: function() {
 295          var category = this.getCategoryById(this.get('activecategoryid')),
 296              listing = this.get('courselisting'),
 297              count = 0;
 298          if (!listing) {
 299              return false;
 300          }
 301  
 302          // Disable course move to bulk action as nothing will be selected on initialise.
 303          var menumovecoursesto = listing.one('#menumovecoursesto');
 304          if (menumovecoursesto) {
 305              menumovecoursesto.setAttribute('disabled', true);
 306          }
 307  
 308          listing.all('.listitem[data-id]').each(function(node) {
 309              this.registerCourse(new Course({
 310                  node: node,
 311                  console: this,
 312                  category: category
 313              }));
 314              count++;
 315          }, this);
 316          listing.delegate('click', this.handleCourseDelegation, 'a[data-action]', this);
 317          listing.delegate('click', this.handleCourseDelegation, 'input[name="bc[]"]', this);
 318          Y.log(count + ' courses being managed', 'info', 'moodle-course-management');
 319      },
 320  
 321      /**
 322       * Registers a course within the management display.
 323       * @method registerCourse
 324       * @param {Course} course
 325       */
 326      registerCourse: function(course) {
 327          var courses = this.get('courses');
 328          courses.push(course);
 329          this.set('courses', courses);
 330      },
 331  
 332      /**
 333       * Handles the event fired by a delegated course listener.
 334       *
 335       * @method handleCourseDelegation
 336       * @protected
 337       * @param {EventFacade} e
 338       */
 339      handleCourseDelegation: function(e) {
 340          var target = e.currentTarget,
 341              action = target.getData('action'),
 342              courseid = target.ancestor('.listitem').getData('id'),
 343              course = this.getCourseById(courseid);
 344          if (course) {
 345              course.handle(action, e);
 346          } else {
 347              Y.log('Course with ID ' + courseid + ' could not be found for delegation', 'error', 'moodle-course-management');
 348          }
 349      },
 350  
 351      /**
 352       * Handles the event fired by a delegated course listener.
 353       *
 354       * @method handleCategoryDelegation
 355       * @protected
 356       * @param {EventFacade} e
 357       */
 358      handleCategoryDelegation: function(e) {
 359          var target = e.currentTarget,
 360              action = target.getData('action'),
 361              categoryid = target.ancestor('.listitem').getData('id'),
 362              category = this.getCategoryById(categoryid);
 363          if (category) {
 364              category.handle(action, e);
 365          } else {
 366              Y.log('Could not find category to delegate to.', 'error', 'moodle-course-management');
 367          }
 368      },
 369  
 370      /**
 371       * Check if any course is selected.
 372       *
 373       * @method isCourseSelected
 374       * @param {Node} checkboxnode Checkbox node on which action happened.
 375       * @return bool
 376       */
 377      isCourseSelected: function(checkboxnode) {
 378          var selected = false;
 379  
 380          // If any course selected then show move to category select box.
 381          if (checkboxnode && checkboxnode.get('checked')) {
 382              selected = true;
 383          } else {
 384              var i,
 385                  course,
 386                  courses = this.get('courses'),
 387                  length = courses.length;
 388              for (i = 0; i < length; i++) {
 389                  if (courses.hasOwnProperty(i)) {
 390                      course = courses[i];
 391                      if (course.get('node').one('input[name="bc[]"]').get('checked')) {
 392                          selected = true;
 393                          break;
 394                      }
 395                  }
 396              }
 397          }
 398          return selected;
 399      },
 400  
 401      /**
 402       * Check if any category is selected.
 403       *
 404       * @method isCategorySelected
 405       * @param {Node} checkboxnode Checkbox node on which action happened.
 406       * @return bool
 407       */
 408      isCategorySelected: function(checkboxnode) {
 409          var selected = false;
 410  
 411          // If any category selected then show move to category select box.
 412          if (checkboxnode && checkboxnode.get('checked')) {
 413              selected = true;
 414          } else {
 415              var i,
 416                  category,
 417                  categories = this.get('categories'),
 418                  length = categories.length;
 419              for (i = 0; i < length; i++) {
 420                  if (categories.hasOwnProperty(i)) {
 421                      category = categories[i];
 422                      if (category.get('node').one('input[name="bcat[]"]').get('checked')) {
 423                          selected = true;
 424                          break;
 425                      }
 426                  }
 427              }
 428          }
 429          return selected;
 430      },
 431  
 432      /**
 433       * Handle bulk sort action.
 434       *
 435       * @method handleBulkSortByaction
 436       * @protected
 437       * @param {EventFacade} e
 438       */
 439      handleBulkSortByaction: function(e) {
 440          var sortcategoryby = this.get('categorylisting').one('#menuresortcategoriesby'),
 441              sortcourseby = this.get('categorylisting').one('#menuresortcoursesby'),
 442              sortbybutton = this.get('categorylisting').one('input[name="bulksort"]'),
 443              sortby = e;
 444  
 445          if (!sortby) {
 446              sortby = this.get('categorylisting').one('#menuselectsortby');
 447          } else {
 448              if (e && e.currentTarget) {
 449                  sortby = e.currentTarget;
 450              }
 451          }
 452  
 453          // If no sortby select found then return as we can't do anything.
 454          if (!sortby) {
 455              return;
 456          }
 457  
 458          if ((this.get('categories').length <= 1) || (!this.isCategorySelected() &&
 459                  (sortby.get("options").item(sortby.get('selectedIndex')).getAttribute('value') === 'selectedcategories'))) {
 460              if (sortcategoryby) {
 461                  sortcategoryby.setAttribute('disabled', true);
 462              }
 463              if (sortcourseby) {
 464                  sortcourseby.setAttribute('disabled', true);
 465              }
 466              if (sortbybutton) {
 467                  sortbybutton.setAttribute('disabled', true);
 468              }
 469          } else {
 470              if (sortcategoryby) {
 471                  sortcategoryby.removeAttribute('disabled');
 472              }
 473              if (sortcourseby) {
 474                  sortcourseby.removeAttribute('disabled');
 475              }
 476              if (sortbybutton) {
 477                  sortbybutton.removeAttribute('disabled');
 478              }
 479          }
 480      },
 481  
 482      /**
 483       * Returns the category with the given ID.
 484       * @method getCategoryById
 485       * @param {Number} id
 486       * @return {Category|Boolean} The category or false if it can't be found.
 487       */
 488      getCategoryById: function(id) {
 489          var i,
 490              category,
 491              categories = this.get('categories'),
 492              length = categories.length;
 493          for (i = 0; i < length; i++) {
 494              if (categories.hasOwnProperty(i)) {
 495                  category = categories[i];
 496                  if (category.get('categoryid') === id) {
 497                      return category;
 498                  }
 499              }
 500          }
 501          return false;
 502      },
 503  
 504      /**
 505       * Returns the course with the given id.
 506       * @method getCourseById
 507       * @param {Number} id
 508       * @return {Course|Boolean} The course or false if not found/
 509       */
 510      getCourseById: function(id) {
 511          var i,
 512              course,
 513              courses = this.get('courses'),
 514              length = courses.length;
 515          for (i = 0; i < length; i++) {
 516              if (courses.hasOwnProperty(i)) {
 517                  course = courses[i];
 518                  if (course.get('courseid') === id) {
 519                      return course;
 520                  }
 521              }
 522          }
 523          return false;
 524      },
 525  
 526      /**
 527       * Removes the course with the given ID.
 528       * @method removeCourseById
 529       * @param {Number} id
 530       */
 531      removeCourseById: function(id) {
 532          var courses = this.get('courses'),
 533              length = courses.length,
 534              course,
 535              i;
 536          for (i = 0; i < length; i++) {
 537              course = courses[i];
 538              if (course.get('courseid') === id) {
 539                  courses.splice(i, 1);
 540                  break;
 541              }
 542          }
 543      },
 544  
 545      /**
 546       * Performs an AJAX action.
 547       *
 548       * @method performAjaxAction
 549       * @param {String} action The action to perform.
 550       * @param {Object} args The arguments to pass through with teh request.
 551       * @param {Function} callback The function to call when all is done.
 552       * @param {Object} context The object to use as the context for the callback.
 553       */
 554      performAjaxAction: function(action, args, callback, context) {
 555          var io = new Y.IO();
 556          args.action = action;
 557          args.ajax = '1';
 558          args.sesskey = M.cfg.sesskey;
 559          if (callback === null) {
 560              callback = function() {
 561                  Y.log("'Action '" + action + "' completed", 'debug', 'moodle-course-management');
 562              };
 563          }
 564          io.send(this.get('ajaxurl'), {
 565              method: 'POST',
 566              on: {
 567                  complete: callback
 568              },
 569              context: context,
 570              data: args,
 571              'arguments': args
 572          });
 573      }
 574  };
 575  Y.extend(Console, Y.Base, Console.prototype);
 576  
 577  M.course = M.course || {};
 578  M.course.management = M.course.management || {};
 579  M.course.management.console = null;
 580  
 581  /**
 582   * Initalises the course management console.
 583   *
 584   * @method M.course.management.init
 585   * @static
 586   * @param {Object} config
 587   */
 588  M.course.management.init = function(config) {
 589      M.course.management.console = new Console(config);
 590  };


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1