/totara/hierarchy/prefix/competency/template/lib.php

https://github.com/moodlehq/totara · PHP · 136 lines · 65 code · 20 blank · 51 comment · 16 complexity · e35a30b05dffa694c225adf2508dbca6 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Totara LMS
  4. *
  5. * Copyright (C) 2010 - 2013 Totara Learning Solutions LTD
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @author Eugene Venter <eugene@catalyst.net.nz>
  21. * @author Simon Coggins <simon.coggins@totaralms.com>
  22. * @package totara
  23. * @subpackage totara_hierarchy
  24. */
  25. /**
  26. * template/lib.php
  27. *
  28. * Library of functions related to competency templates.
  29. *
  30. * Note: Functions in this library should have names beginning with "competency_template",
  31. * in order to avoid name collisions
  32. *
  33. */
  34. /**
  35. * A function to display a table list of competency templates
  36. *
  37. * @param array $templates
  38. * @param int $frameworkid
  39. * @return html
  40. */
  41. function competency_template_display_table($templates, $frameworkid) {
  42. global $OUTPUT;
  43. $sitecontext = context_system::instance();
  44. $editing = optional_param('edit', -1, PARAM_BOOL);
  45. // Cache user capabilities
  46. $can_add = has_capability('totara/hierarchy:createcompetencytemplate', $sitecontext);
  47. $can_edit = has_capability('totara/hierarchy:updatecompetencytemplate', $sitecontext);
  48. $can_delete = has_capability('totara/hierarchy:deletecompetencytemplate', $sitecontext);
  49. if (($can_add || $can_edit || $can_delete) && $editing) {
  50. $editingon = $USER->templateediting = 1;
  51. } else {
  52. $editingon = $USER->templateediting = 0;
  53. }
  54. ///
  55. /// Generate / display page
  56. ///
  57. $str_edit = get_string('edit');
  58. $str_delete = get_string('delete');
  59. $str_hide = get_string('hide');
  60. $str_show = get_string('show');
  61. if ($templates) {
  62. // Create display table
  63. $table = new html_table();
  64. // Setup column headers
  65. $table->head = array();
  66. $table->align = array();
  67. $table->head[] = get_string('template', 'totara_hierarchy');
  68. $table->align[] = 'left';
  69. $table->head[] = get_string('competencies', 'totara_hierarchy');
  70. $table->align[] = 'center';
  71. $table->head[] = get_string('createdon', 'totara_hierarchy');
  72. $table->align[] = 'left';
  73. // Add edit column
  74. if ($editingon && $can_edit) {
  75. $table->head[] = get_string('edit');
  76. $table->align[] = 'center';
  77. }
  78. // Add rows to table
  79. foreach ($templates as $template) {
  80. $row = array();
  81. $cssclass = !$template->visible ? 'dimmed' : '';
  82. $row[] = $OUTPUT->action_link(new moodle_url('prefix/competency/template/view.php', array('id' => $template->id)), $template->fullname, null, array('class' => $cssclass));
  83. $row[] = $OUTPUT->action_link(new moodle_url('prefix/competency/template/view.php', array('id' => $template->id)), $template->competencycount, null, array('class' => $cssclass));
  84. $row[] = userdate($template->timecreated, get_string('strftimedaydate', 'langconfig'));
  85. // Add edit link
  86. $buttons = array();
  87. if ($editingon && $can_edit) {
  88. $buttons[] = $OUTPUT->action_icon(new moodle_url('prefix/competency/template/edit.php', array('id' => $template->id)),
  89. new pix_icon('t/edit.gif', $stredit), null, array('class' => 'iconsmall', 'title' => $str_edit));
  90. }
  91. if ($editingon && $can_delete) {
  92. $buttons[] = $OUTPUT->action_icon(new moodle_url('prefix/competency/template/delete.php', array('id' => $template->id)),
  93. new pix_icon('t/delete.gif', $strdelete), null, array('class' => 'iconsmall', 'title' => $str_delete));
  94. }
  95. if ($buttons) {
  96. $row[] = implode($buttons, '');
  97. }
  98. $table->data[] = $row;
  99. }
  100. }
  101. // Display page
  102. echo $OUTPUT->heading(get_string('competencytemplates', 'totara_hierarchy'));
  103. if ($templates) {
  104. echo html_writer::table($table);
  105. } else {
  106. echo html_writer::tag('p', get_string('notemplateinframework', 'totara_hierarchy'));
  107. }
  108. // Editing buttons
  109. if ($can_add) {
  110. $data = array('frameworkid' => $frameworkid);
  111. // Print button for creating new template
  112. echo html_writer::tag('div',
  113. $OUTPUT->single_button(new moodle_url('prefix/competency/template/edit.php', $data), get_string('addnewtemplate', 'totara_hierarchy'), 'get'),
  114. array('class' => 'buttons'));
  115. }
  116. }