Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/mod/data/ -> view.php (source)

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  ///////////////////////////////////////////////////////////////////////////
   3  //                                                                       //
   4  // NOTICE OF COPYRIGHT                                                   //
   5  //                                                                       //
   6  // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
   7  //          http://moodle.org                                            //
   8  //                                                                       //
   9  // Copyright (C) 2005 Martin Dougiamas  http://dougiamas.com             //
  10  //                                                                       //
  11  // This program is free software; you can redistribute it and/or modify  //
  12  // it under the terms of the GNU General Public License as published by  //
  13  // the Free Software Foundation; either version 2 of the License, or     //
  14  // (at your option) any later version.                                   //
  15  //                                                                       //
  16  // This program is distributed in the hope that it will be useful,       //
  17  // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
  18  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
  19  // GNU General Public License for more details:                          //
  20  //                                                                       //
  21  //          http://www.gnu.org/copyleft/gpl.html                         //
  22  //                                                                       //
  23  ///////////////////////////////////////////////////////////////////////////
  24  
  25      require_once(__DIR__ . '/../../config.php');
  26      require_once($CFG->dirroot . '/mod/data/locallib.php');
  27      require_once($CFG->libdir . '/rsslib.php');
  28  
  29  /// One of these is necessary!
  30      $id = optional_param('id', 0, PARAM_INT);  // course module id
  31      $d = optional_param('d', 0, PARAM_INT);   // database id
  32      $rid = optional_param('rid', 0, PARAM_INT);    //record id
  33      $mode = optional_param('mode', '', PARAM_ALPHA);    // Force the browse mode  ('single')
  34      $filter = optional_param('filter', 0, PARAM_BOOL);
  35      // search filter will only be applied when $filter is true
  36  
  37      $edit = optional_param('edit', -1, PARAM_BOOL);
  38      $page = optional_param('page', 0, PARAM_INT);
  39  /// These can be added to perform an action on a record
  40      $approve = optional_param('approve', 0, PARAM_INT);    //approval recordid
  41      $disapprove = optional_param('disapprove', 0, PARAM_INT);    // disapproval recordid
  42      $delete = optional_param('delete', 0, PARAM_INT);    //delete recordid
  43      $multidelete = optional_param_array('delcheck', null, PARAM_INT);
  44      $serialdelete = optional_param('serialdelete', null, PARAM_RAW);
  45  
  46      if ($id) {
  47          if (! $cm = get_coursemodule_from_id('data', $id)) {
  48              print_error('invalidcoursemodule');
  49          }
  50          if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
  51              print_error('coursemisconf');
  52          }
  53          if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
  54              print_error('invalidcoursemodule');
  55          }
  56          $record = NULL;
  57  
  58      } else if ($rid) {
  59          if (! $record = $DB->get_record('data_records', array('id'=>$rid))) {
  60              print_error('invalidrecord', 'data');
  61          }
  62          if (! $data = $DB->get_record('data', array('id'=>$record->dataid))) {
  63              print_error('invalidid', 'data');
  64          }
  65          if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
  66              print_error('coursemisconf');
  67          }
  68          if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
  69              print_error('invalidcoursemodule');
  70          }
  71      } else {   // We must have $d
  72          if (! $data = $DB->get_record('data', array('id'=>$d))) {
  73              print_error('invalidid', 'data');
  74          }
  75          if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
  76              print_error('coursemisconf');
  77          }
  78          if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
  79              print_error('invalidcoursemodule');
  80          }
  81          $record = NULL;
  82      }
  83      $cm = cm_info::create($cm);
  84      require_course_login($course, true, $cm);
  85  
  86      require_once($CFG->dirroot . '/comment/lib.php');
  87      comment::init();
  88  
  89      $context = context_module::instance($cm->id);
  90      require_capability('mod/data:viewentry', $context);
  91  
  92  /// If we have an empty Database then redirect because this page is useless without data
  93      if (has_capability('mod/data:managetemplates', $context)) {
  94          if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {      // Brand new database!
  95              redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);  // Redirect to field entry
  96          }
  97      }
  98  
  99  
 100  /// Check further parameters that set browsing preferences
 101      if (!isset($SESSION->dataprefs)) {
 102          $SESSION->dataprefs = array();
 103      }
 104      if (!isset($SESSION->dataprefs[$data->id])) {
 105          $SESSION->dataprefs[$data->id] = array();
 106          $SESSION->dataprefs[$data->id]['search'] = '';
 107          $SESSION->dataprefs[$data->id]['search_array'] = array();
 108          $SESSION->dataprefs[$data->id]['sort'] = $data->defaultsort;
 109          $SESSION->dataprefs[$data->id]['advanced'] = 0;
 110          $SESSION->dataprefs[$data->id]['order'] = ($data->defaultsortdir == 0) ? 'ASC' : 'DESC';
 111      }
 112  
 113      // reset advanced form
 114      if (!is_null(optional_param('resetadv', null, PARAM_RAW))) {
 115          $SESSION->dataprefs[$data->id]['search_array'] = array();
 116          // we need the redirect to cleanup the form state properly
 117          redirect("view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=1");
 118      }
 119  
 120      $advanced = optional_param('advanced', -1, PARAM_INT);
 121      if ($advanced == -1) {
 122          $advanced = $SESSION->dataprefs[$data->id]['advanced'];
 123      } else {
 124          if (!$advanced) {
 125              // explicitly switched to normal mode - discard all advanced search settings
 126              $SESSION->dataprefs[$data->id]['search_array'] = array();
 127          }
 128          $SESSION->dataprefs[$data->id]['advanced'] = $advanced;
 129      }
 130  
 131      $search_array = $SESSION->dataprefs[$data->id]['search_array'];
 132  
 133      if (!empty($advanced)) {
 134          $search = '';
 135  
 136          //Added to ammend paging error. This error would occur when attempting to go from one page of advanced
 137          //search results to another.  All fields were reset in the page transfer, and there was no way of determining
 138          //whether or not the user reset them.  This would cause a blank search to execute whenever the user attempted
 139          //to see any page of results past the first.
 140          //This fix works as follows:
 141          //$paging flag is set to false when page 0 of the advanced search results is viewed for the first time.
 142          //Viewing any page of results after page 0 passes the false $paging flag though the URL (see line 523) and the
 143          //execution falls through to the second condition below, allowing paging to be set to true.
 144          //Paging remains true and keeps getting passed though the URL until a new search is performed
 145          //(even if page 0 is revisited).
 146          //A false $paging flag generates advanced search results based on the fields input by the user.
 147          //A true $paging flag generates davanced search results from the $SESSION global.
 148  
 149          $paging = optional_param('paging', NULL, PARAM_BOOL);
 150          if($page == 0 && !isset($paging)) {
 151              $paging = false;
 152          }
 153          else {
 154              $paging = true;
 155          }
 156  
 157          // Now build the advanced search array.
 158          list($search_array, $search) = data_build_search_array($data, $paging, $search_array);
 159          $SESSION->dataprefs[$data->id]['search_array'] = $search_array;     // Make it sticky.
 160  
 161      } else {
 162          $search = optional_param('search', $SESSION->dataprefs[$data->id]['search'], PARAM_NOTAGS);
 163          //Paging variable not used for standard search. Set it to null.
 164          $paging = NULL;
 165      }
 166  
 167      // Disable search filters if $filter is not true:
 168      if (! $filter) {
 169          $search = '';
 170      }
 171  
 172      $SESSION->dataprefs[$data->id]['search'] = $search;   // Make it sticky
 173  
 174      $sort = optional_param('sort', $SESSION->dataprefs[$data->id]['sort'], PARAM_INT);
 175      $SESSION->dataprefs[$data->id]['sort'] = $sort;       // Make it sticky
 176  
 177      $order = (optional_param('order', $SESSION->dataprefs[$data->id]['order'], PARAM_ALPHA) == 'ASC') ? 'ASC': 'DESC';
 178      $SESSION->dataprefs[$data->id]['order'] = $order;     // Make it sticky
 179  
 180  
 181      $oldperpage = get_user_preferences('data_perpage_'.$data->id, 10);
 182      $perpage = optional_param('perpage', $oldperpage, PARAM_INT);
 183  
 184      if ($perpage < 2) {
 185          $perpage = 2;
 186      }
 187      if ($perpage != $oldperpage) {
 188          set_user_preference('data_perpage_'.$data->id, $perpage);
 189      }
 190  
 191      // Completion and trigger events.
 192      data_view($data, $course, $cm, $context);
 193  
 194      $urlparams = array('d' => $data->id);
 195      if ($record) {
 196          $urlparams['rid'] = $record->id;
 197      }
 198      if ($page) {
 199          $urlparams['page'] = $page;
 200      }
 201      if ($mode) {
 202          $urlparams['mode'] = $mode;
 203      }
 204      if ($filter) {
 205          $urlparams['filter'] = $filter;
 206      }
 207  // Initialize $PAGE, compute blocks
 208      $PAGE->set_url('/mod/data/view.php', $urlparams);
 209  
 210      if (($edit != -1) and $PAGE->user_allowed_editing()) {
 211          $USER->editing = $edit;
 212      }
 213  
 214      $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
 215  
 216  /// RSS and CSS and JS meta
 217      $meta = '';
 218      if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
 219          $rsstitle = $courseshortname . ': ' . format_string($data->name);
 220          rss_add_http_header($context, 'mod_data', $data, $rsstitle);
 221      }
 222      if ($data->csstemplate) {
 223          $PAGE->requires->css('/mod/data/css.php?d='.$data->id);
 224      }
 225      if ($data->jstemplate) {
 226          $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
 227      }
 228  
 229  /// Print the page header
 230      // Note: MDL-19010 there will be further changes to printing header and blocks.
 231      // The code will be much nicer than this eventually.
 232      $title = $courseshortname.': ' . format_string($data->name);
 233  
 234      if ($PAGE->user_allowed_editing()) {
 235          // Change URL parameter and block display string value depending on whether editing is enabled or not
 236          if ($PAGE->user_is_editing()) {
 237              $urlediting = 'off';
 238              $strediting = get_string('blockseditoff');
 239          } else {
 240              $urlediting = 'on';
 241              $strediting = get_string('blocksediton');
 242          }
 243          $url = new moodle_url($CFG->wwwroot.'/mod/data/view.php', array('id' => $cm->id, 'edit' => $urlediting));
 244          $PAGE->set_button($OUTPUT->single_button($url, $strediting));
 245      }
 246  
 247      if ($mode == 'asearch') {
 248          $PAGE->navbar->add(get_string('search'));
 249      }
 250  
 251      $PAGE->force_settings_menu();
 252      $PAGE->set_title($title);
 253      $PAGE->set_heading($course->fullname);
 254  
 255      echo $OUTPUT->header();
 256  
 257      // Check to see if groups are being used here.
 258      // We need the most up to date current group value. Make sure it is updated at this point.
 259      $currentgroup = groups_get_activity_group($cm, true);
 260      $groupmode = groups_get_activity_groupmode($cm);
 261      $canmanageentries = has_capability('mod/data:manageentries', $context);
 262  
 263  
 264      // Detect entries not approved yet and show hint instead of not found error.
 265      if ($record and !data_can_view_record($data, $record, $currentgroup, $canmanageentries)) {
 266          print_error('notapproved', 'data');
 267      }
 268  
 269      echo $OUTPUT->heading(format_string($data->name), 2);
 270  
 271      // Render the activity information.
 272      $completiondetails = \core_completion\cm_completion_details::get_instance($cm, $USER->id);
 273      $activitydates = \core\activity_dates::get_dates_for_module($cm, $USER->id);
 274      echo $OUTPUT->activity_information($cm, $completiondetails, $activitydates);
 275  
 276      // Do we need to show a link to the RSS feed for the records?
 277      //this links has been Settings (database activity administration) block
 278      /*if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
 279          echo '<div style="float:right;">';
 280          rss_print_link($context->id, $USER->id, 'mod_data', $data->id, get_string('rsstype'));
 281          echo '</div>';
 282          echo '<div style="clear:both;"></div>';
 283      }*/
 284  
 285      if ($data->intro and empty($page) and empty($record) and $mode != 'single') {
 286          $options = new stdClass();
 287          $options->noclean = true;
 288      }
 289      echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
 290  
 291      $returnurl = $CFG->wwwroot . '/mod/data/view.php?d='.$data->id.'&amp;search='.s($search).'&amp;sort='.s($sort).'&amp;order='.s($order).'&amp;';
 292      groups_print_activity_menu($cm, $returnurl);
 293  
 294  /// Delete any requested records
 295  
 296      if ($delete && confirm_sesskey() && (data_user_can_manage_entry($delete, $data, $context))) {
 297          if ($confirm = optional_param('confirm',0,PARAM_INT)) {
 298              if (data_delete_record($delete, $data, $course->id, $cm->id)) {
 299                  echo $OUTPUT->notification(get_string('recorddeleted','data'), 'notifysuccess');
 300              }
 301          } else {   // Print a confirmation page
 302              $userfieldsapi = \core_user\fields::for_userpic()->excluding('id');
 303              $allnamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
 304              $dbparams = array($delete);
 305              if ($deleterecord = $DB->get_record_sql("SELECT dr.*, $allnamefields
 306                                                         FROM {data_records} dr
 307                                                              JOIN {user} u ON dr.userid = u.id
 308                                                        WHERE dr.id = ?", $dbparams, MUST_EXIST)) { // Need to check this is valid.
 309                  if ($deleterecord->dataid == $data->id) {                       // Must be from this database
 310                      $deletebutton = new single_button(new moodle_url('/mod/data/view.php?d='.$data->id.'&delete='.$delete.'&confirm=1'), get_string('delete'), 'post');
 311                      echo $OUTPUT->confirm(get_string('confirmdeleterecord','data'),
 312                              $deletebutton, 'view.php?d='.$data->id);
 313  
 314                      $records[] = $deleterecord;
 315                      echo data_print_template('singletemplate', $records, $data, '', 0, true);
 316  
 317                      echo $OUTPUT->footer();
 318                      exit;
 319                  }
 320              }
 321          }
 322      }
 323  
 324  
 325      // Multi-delete.
 326      if ($serialdelete) {
 327          $multidelete = json_decode($serialdelete);
 328      }
 329  
 330      if ($multidelete && confirm_sesskey() && $canmanageentries) {
 331          if ($confirm = optional_param('confirm', 0, PARAM_INT)) {
 332              foreach ($multidelete as $value) {
 333                  data_delete_record($value, $data, $course->id, $cm->id);
 334              }
 335          } else {
 336              $validrecords = array();
 337              $recordids = array();
 338              foreach ($multidelete as $value) {
 339                  $userfieldsapi = \core_user\fields::for_userpic()->excluding('id');
 340                  $allnamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
 341                  $dbparams = array('id' => $value);
 342                  if ($deleterecord = $DB->get_record_sql("SELECT dr.*, $allnamefields
 343                                                             FROM {data_records} dr
 344                                                             JOIN {user} u ON dr.userid = u.id
 345                                                            WHERE dr.id = ?", $dbparams)) { // Need to check this is valid.
 346                      if ($deleterecord->dataid == $data->id) {  // Must be from this database.
 347                          $validrecords[] = $deleterecord;
 348                          $recordids[] = $deleterecord->id;
 349                      }
 350                  }
 351              }
 352              $serialiseddata = json_encode($recordids);
 353              $submitactions = array('d' => $data->id, 'sesskey' => sesskey(), 'confirm' => '1', 'serialdelete' => $serialiseddata);
 354              $action = new moodle_url('/mod/data/view.php', $submitactions);
 355              $cancelurl = new moodle_url('/mod/data/view.php', array('d' => $data->id));
 356              $deletebutton = new single_button($action, get_string('delete'));
 357              echo $OUTPUT->confirm(get_string('confirmdeleterecords', 'data'), $deletebutton, $cancelurl);
 358              echo data_print_template('listtemplate', $validrecords, $data, '', 0, false);
 359              echo $OUTPUT->footer();
 360              exit;
 361          }
 362      }
 363  
 364      // If data activity closed dont let students in.
 365      // No need to display warnings because activity dates are displayed at the top of the page.
 366      list($showactivity, $warnings) = data_get_time_availability_status($data, $canmanageentries);
 367  
 368  if ($showactivity) {
 369      // Print the tabs
 370      if ($record or $mode == 'single') {
 371          $currenttab = 'single';
 372      } elseif($mode == 'asearch') {
 373          $currenttab = 'asearch';
 374      }
 375      else {
 376          $currenttab = 'list';
 377      }
 378      include ('tabs.php');
 379  
 380      if ($mode == 'asearch') {
 381          $maxcount = 0;
 382          data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode);
 383  
 384      } else {
 385          // Approve or disapprove any requested records
 386          $approvecap = has_capability('mod/data:approve', $context);
 387  
 388          if (($approve || $disapprove) && confirm_sesskey() && $approvecap) {
 389              $newapproved = $approve ? true : false;
 390              $recordid = $newapproved ? $approve : $disapprove;
 391              if ($approverecord = $DB->get_record('data_records', array('id' => $recordid))) {   // Need to check this is valid
 392                  if ($approverecord->dataid == $data->id) {                       // Must be from this database
 393                      data_approve_entry($approverecord->id, $newapproved);
 394                      $msgkey = $newapproved ? 'recordapproved' : 'recorddisapproved';
 395                      echo $OUTPUT->notification(get_string($msgkey, 'data'), 'notifysuccess');
 396                  }
 397              }
 398          }
 399  
 400          $numentries = data_numentries($data);
 401      /// Check the number of entries required against the number of entries already made (doesn't apply to teachers)
 402          if ($data->entriesleft = data_get_entries_left_to_add($data, $numentries, $canmanageentries)) {
 403              $strentrieslefttoadd = get_string('entrieslefttoadd', 'data', $data);
 404              echo $OUTPUT->notification($strentrieslefttoadd);
 405          }
 406  
 407      /// Check the number of entries required before to view other participant's entries against the number of entries already made (doesn't apply to teachers)
 408          $requiredentries_allowed = true;
 409          if ($data->entrieslefttoview = data_get_entries_left_to_view($data, $numentries, $canmanageentries)) {
 410              $strentrieslefttoaddtoview = get_string('entrieslefttoaddtoview', 'data', $data);
 411              echo $OUTPUT->notification($strentrieslefttoaddtoview);
 412              $requiredentries_allowed = false;
 413          }
 414  
 415          // Search for entries.
 416          list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
 417              data_search_entries($data, $cm, $context, $mode, $currentgroup, $search, $sort, $order, $page, $perpage, $advanced, $search_array, $record);
 418  
 419          // Advanced search form doesn't make sense for single (redirects list view).
 420          if ($maxcount && $mode != 'single') {
 421              data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode);
 422          }
 423  
 424          if (empty($records)) {
 425              if ($maxcount){
 426                  $a = new stdClass();
 427                  $a->max = $maxcount;
 428                  $a->reseturl = "view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=0";
 429                  echo $OUTPUT->notification(get_string('foundnorecords','data', $a));
 430              } else {
 431                  echo $OUTPUT->notification(get_string('norecords','data'));
 432              }
 433  
 434          } else {
 435              //  We have some records to print.
 436              $url = new moodle_url('/mod/data/view.php', array('d' => $data->id, 'sesskey' => sesskey()));
 437              echo html_writer::start_tag('form', array('action' => $url, 'method' => 'post'));
 438  
 439              if ($maxcount != $totalcount) {
 440                  $a = new stdClass();
 441                  $a->num = $totalcount;
 442                  $a->max = $maxcount;
 443                  $a->reseturl = "view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=0";
 444                  echo $OUTPUT->notification(get_string('foundrecords', 'data', $a), 'notifysuccess');
 445              }
 446  
 447              if ($mode == 'single') { // Single template
 448                  $baseurl = 'view.php?d=' . $data->id . '&mode=single&';
 449                  if (!empty($search)) {
 450                      $baseurl .= 'filter=1&';
 451                  }
 452                  if (!empty($page)) {
 453                      $baseurl .= 'page=' . $page;
 454                  }
 455                  echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl);
 456  
 457                  if (empty($data->singletemplate)){
 458                      echo $OUTPUT->notification(get_string('nosingletemplate','data'));
 459                      data_generate_default_template($data, 'singletemplate', 0, false, false);
 460                  }
 461  
 462                  //data_print_template() only adds ratings for singletemplate which is why we're attaching them here
 463                  //attach ratings to data records
 464                  require_once($CFG->dirroot.'/rating/lib.php');
 465                  if ($data->assessed != RATING_AGGREGATE_NONE) {
 466                      $ratingoptions = new stdClass;
 467                      $ratingoptions->context = $context;
 468                      $ratingoptions->component = 'mod_data';
 469                      $ratingoptions->ratingarea = 'entry';
 470                      $ratingoptions->items = $records;
 471                      $ratingoptions->aggregate = $data->assessed;//the aggregation method
 472                      $ratingoptions->scaleid = $data->scale;
 473                      $ratingoptions->userid = $USER->id;
 474                      $ratingoptions->returnurl = $CFG->wwwroot.'/mod/data/'.$baseurl;
 475                      $ratingoptions->assesstimestart = $data->assesstimestart;
 476                      $ratingoptions->assesstimefinish = $data->assesstimefinish;
 477  
 478                      $rm = new rating_manager();
 479                      $records = $rm->get_ratings($ratingoptions);
 480                  }
 481  
 482                  data_print_template('singletemplate', $records, $data, $search, $page, false, new moodle_url($baseurl));
 483  
 484                  echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl);
 485  
 486              } else {                                  // List template
 487                  $baseurl = 'view.php?d='.$data->id.'&amp;';
 488                  //send the advanced flag through the URL so it is remembered while paging.
 489                  $baseurl .= 'advanced='.$advanced.'&amp;';
 490                  if (!empty($search)) {
 491                      $baseurl .= 'filter=1&amp;';
 492                  }
 493                  //pass variable to allow determining whether or not we are paging through results.
 494                  $baseurl .= 'paging='.$paging.'&amp;';
 495  
 496                  echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl);
 497  
 498                  if (empty($data->listtemplate)){
 499                      echo $OUTPUT->notification(get_string('nolisttemplate','data'));
 500                      data_generate_default_template($data, 'listtemplate', 0, false, false);
 501                  }
 502                  echo $data->listtemplateheader;
 503                  data_print_template('listtemplate', $records, $data, $search, $page, false, new moodle_url($baseurl));
 504                  echo $data->listtemplatefooter;
 505  
 506                  echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl);
 507              }
 508  
 509              if ($mode != 'single' && $canmanageentries) {
 510                  // Build the select/deselect all control.
 511                  $selectallid = 'selectall-listview-entries';
 512                  $togglegroup = 'listview-entries';
 513                  $mastercheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [
 514                      'id' => $selectallid,
 515                      'name' => $selectallid,
 516                      'value' => 1,
 517                      'label' => get_string('selectall'),
 518                      'classes' => 'btn-secondary mr-1',
 519                  ], true);
 520                  echo $OUTPUT->render($mastercheckbox);
 521  
 522                  $deleteselected = html_writer::empty_tag('input', array(
 523                      'class' => 'btn btn-secondary',
 524                      'type' => 'submit',
 525                      'value' => get_string('deleteselected'),
 526                      'disabled' => true,
 527                      'data-action' => 'toggle',
 528                      'data-togglegroup' => $togglegroup,
 529                      'data-toggle' => 'action',
 530                  ));
 531                  echo $deleteselected;
 532              }
 533  
 534              echo html_writer::end_tag('form');
 535          }
 536      }
 537  
 538      $search = trim($search);
 539      if (empty($records)) {
 540          $records = array();
 541      }
 542  
 543      // Check to see if we can export records to a portfolio. This is for exporting all records, not just the ones in the search.
 544      if ($mode == '' && !empty($CFG->enableportfolios) && !empty($records)) {
 545          $canexport = false;
 546          // Exportallentries and exportentry are basically the same capability.
 547          if (has_capability('mod/data:exportallentries', $context) || has_capability('mod/data:exportentry', $context)) {
 548              $canexport = true;
 549          } else if (has_capability('mod/data:exportownentry', $context) &&
 550                  $DB->record_exists('data_records', array('userid' => $USER->id))) {
 551              $canexport = true;
 552          }
 553          if ($canexport) {
 554              require_once($CFG->libdir . '/portfoliolib.php');
 555              $button = new portfolio_add_button();
 556              $button->set_callback_options('data_portfolio_caller', array('id' => $cm->id), 'mod_data');
 557              if (data_portfolio_caller::has_files($data)) {
 558                  $button->set_formats(array(PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_LEAP2A)); // No plain html for us.
 559              }
 560              echo $button->to_html(PORTFOLIO_ADD_FULL_FORM);
 561          }
 562      }
 563  }
 564  
 565  echo $OUTPUT->footer();