[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/glossary/ -> view.php (source)

   1  <?php
   2  
   3  /// This page prints a particular instance of glossary
   4  require_once("../../config.php");
   5  require_once ("lib.php");
   6  require_once($CFG->libdir . '/completionlib.php');
   7  require_once("$CFG->libdir/rsslib.php");
   8  
   9  $id = optional_param('id', 0, PARAM_INT);           // Course Module ID
  10  $g  = optional_param('g', 0, PARAM_INT);            // Glossary ID
  11  
  12  $tab  = optional_param('tab', GLOSSARY_NO_VIEW, PARAM_ALPHA);    // browsing entries by categories?
  13  $displayformat = optional_param('displayformat',-1, PARAM_INT);  // override of the glossary display format
  14  
  15  $mode       = optional_param('mode', '', PARAM_ALPHA);           // term entry cat date letter search author approval
  16  $hook       = optional_param('hook', '', PARAM_CLEAN);           // the term, entry, cat, etc... to look for based on mode
  17  $fullsearch = optional_param('fullsearch', 0,PARAM_INT);         // full search (concept and definition) when searching?
  18  $sortkey    = optional_param('sortkey', '', PARAM_ALPHA);// Sorted view: CREATION | UPDATE | FIRSTNAME | LASTNAME...
  19  $sortorder  = optional_param('sortorder', 'ASC', PARAM_ALPHA);   // it defines the order of the sorting (ASC or DESC)
  20  $offset     = optional_param('offset', 0,PARAM_INT);             // entries to bypass (for paging purposes)
  21  $page       = optional_param('page', 0,PARAM_INT);               // Page to show (for paging purposes)
  22  $show       = optional_param('show', '', PARAM_ALPHA);           // [ concept | alias ] => mode=term hook=$show
  23  
  24  if (!empty($id)) {
  25      if (! $cm = get_coursemodule_from_id('glossary', $id)) {
  26          print_error('invalidcoursemodule');
  27      }
  28      if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
  29          print_error('coursemisconf');
  30      }
  31      if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
  32          print_error('invalidid', 'glossary');
  33      }
  34  
  35  } else if (!empty($g)) {
  36      if (! $glossary = $DB->get_record("glossary", array("id"=>$g))) {
  37          print_error('invalidid', 'glossary');
  38      }
  39      if (! $course = $DB->get_record("course", array("id"=>$glossary->course))) {
  40          print_error('invalidcourseid');
  41      }
  42      if (!$cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id)) {
  43          print_error('invalidcoursemodule');
  44      }
  45      $id = $cm->id;
  46  } else {
  47      print_error('invalidid', 'glossary');
  48  }
  49  
  50  require_course_login($course->id, true, $cm);
  51  $context = context_module::instance($cm->id);
  52  require_capability('mod/glossary:view', $context);
  53  
  54  // Prepare format_string/text options
  55  $fmtoptions = array(
  56      'context' => $context);
  57  
  58  require_once($CFG->dirroot . '/comment/lib.php');
  59  comment::init();
  60  
  61  /// redirecting if adding a new entry
  62  if ($tab == GLOSSARY_ADDENTRY_VIEW ) {
  63      redirect("edit.php?cmid=$cm->id&amp;mode=$mode");
  64  }
  65  
  66  /// setting the defaut number of entries per page if not set
  67  if ( !$entriesbypage = $glossary->entbypage ) {
  68      $entriesbypage = $CFG->glossary_entbypage;
  69  }
  70  
  71  /// If we have received a page, recalculate offset
  72  if ($page != 0 && $offset == 0) {
  73      $offset = $page * $entriesbypage;
  74  }
  75  
  76  /// setting the default values for the display mode of the current glossary
  77  /// only if the glossary is viewed by the first time
  78  if ( $dp = $DB->get_record('glossary_formats', array('name'=>$glossary->displayformat)) ) {
  79  /// Based on format->defaultmode, we build the defaulttab to be showed sometimes
  80      $showtabs = glossary_get_visible_tabs($dp);
  81      switch ($dp->defaultmode) {
  82          case 'cat':
  83              $defaulttab = GLOSSARY_CATEGORY_VIEW;
  84  
  85              // Handle defaultmode if 'category' tab is disabled. Fallback to 'standard' tab.
  86              if (!in_array(GLOSSARY_CATEGORY, $showtabs)) {
  87                  $defaulttab = GLOSSARY_STANDARD_VIEW;
  88              }
  89  
  90              break;
  91          case 'date':
  92              $defaulttab = GLOSSARY_DATE_VIEW;
  93  
  94              // Handle defaultmode if 'date' tab is disabled. Fallback to 'standard' tab.
  95              if (!in_array(GLOSSARY_DATE, $showtabs)) {
  96                  $defaulttab = GLOSSARY_STANDARD_VIEW;
  97              }
  98  
  99              break;
 100          case 'author':
 101              $defaulttab = GLOSSARY_AUTHOR_VIEW;
 102  
 103              // Handle defaultmode if 'author' tab is disabled. Fallback to 'standard' tab.
 104              if (!in_array(GLOSSARY_AUTHOR, $showtabs)) {
 105                  $defaulttab = GLOSSARY_STANDARD_VIEW;
 106              }
 107  
 108              break;
 109          default:
 110              $defaulttab = GLOSSARY_STANDARD_VIEW;
 111      }
 112  /// Fetch the rest of variables
 113      $printpivot = $dp->showgroup;
 114      if ( $mode == '' and $hook == '' and $show == '') {
 115          $mode      = $dp->defaultmode;
 116          $hook      = $dp->defaulthook;
 117          $sortkey   = $dp->sortkey;
 118          $sortorder = $dp->sortorder;
 119      }
 120  } else {
 121      $defaulttab = GLOSSARY_STANDARD_VIEW;
 122      $showtabs = array($defaulttab);
 123      $printpivot = 1;
 124      if ( $mode == '' and $hook == '' and $show == '') {
 125          $mode = 'letter';
 126          $hook = 'ALL';
 127      }
 128  }
 129  
 130  if ( $displayformat == -1 ) {
 131       $displayformat = $glossary->displayformat;
 132  }
 133  
 134  if ( $show ) {
 135      $mode = 'term';
 136      $hook = $show;
 137      $show = '';
 138  }
 139  
 140  /// stablishing flag variables
 141  if ( $sortorder = strtolower($sortorder) ) {
 142      if ($sortorder != 'asc' and $sortorder != 'desc') {
 143          $sortorder = '';
 144      }
 145  }
 146  if ( $sortkey = strtoupper($sortkey) ) {
 147      if ($sortkey != 'CREATION' and
 148          $sortkey != 'UPDATE' and
 149          $sortkey != 'FIRSTNAME' and
 150          $sortkey != 'LASTNAME'
 151          ) {
 152          $sortkey = '';
 153      }
 154  }
 155  
 156  switch ( $mode = strtolower($mode) ) {
 157  case 'search': /// looking for terms containing certain word(s)
 158      $tab = GLOSSARY_STANDARD_VIEW;
 159  
 160      //Clean a bit the search string
 161      $hook = trim(strip_tags($hook));
 162  
 163  break;
 164  
 165  case 'entry':  /// Looking for a certain entry id
 166      $tab = GLOSSARY_STANDARD_VIEW;
 167      if ( $dp = $DB->get_record("glossary_formats", array("name"=>$glossary->displayformat)) ) {
 168          $displayformat = $dp->popupformatname;
 169      }
 170  break;
 171  
 172  case 'cat':    /// Looking for a certain cat
 173      $tab = GLOSSARY_CATEGORY_VIEW;
 174  
 175      // Validation - we don't want to display 'category' tab if it is disabled.
 176      if (!in_array(GLOSSARY_CATEGORY, $showtabs)) {
 177          $tab = GLOSSARY_STANDARD_VIEW;
 178      }
 179  
 180      if ( $hook > 0 ) {
 181          $category = $DB->get_record("glossary_categories", array("id"=>$hook));
 182      }
 183  break;
 184  
 185  case 'approval':    /// Looking for entries waiting for approval
 186      $tab = GLOSSARY_APPROVAL_VIEW;
 187      // Override the display format with the approvaldisplayformat
 188      if ($glossary->approvaldisplayformat !== 'default' && ($df = $DB->get_record("glossary_formats",
 189              array("name" => $glossary->approvaldisplayformat)))) {
 190          $displayformat = $df->popupformatname;
 191      }
 192      if ( !$hook and !$sortkey and !$sortorder) {
 193          $hook = 'ALL';
 194      }
 195  break;
 196  
 197  case 'term':   /// Looking for entries that include certain term in its concept, definition or aliases
 198      $tab = GLOSSARY_STANDARD_VIEW;
 199  break;
 200  
 201  case 'date':
 202      $tab = GLOSSARY_DATE_VIEW;
 203  
 204      // Validation - we dont want to display 'date' tab if it is disabled.
 205      if (!in_array(GLOSSARY_DATE, $showtabs)) {
 206          $tab = GLOSSARY_STANDARD_VIEW;
 207      }
 208  
 209      if ( !$sortkey ) {
 210          $sortkey = 'UPDATE';
 211      }
 212      if ( !$sortorder ) {
 213          $sortorder = 'desc';
 214      }
 215  break;
 216  
 217  case 'author':  /// Looking for entries, browsed by author
 218      $tab = GLOSSARY_AUTHOR_VIEW;
 219  
 220      // Validation - we dont want to display 'author' tab if it is disabled.
 221      if (!in_array(GLOSSARY_AUTHOR, $showtabs)) {
 222          $tab = GLOSSARY_STANDARD_VIEW;
 223      }
 224  
 225      if ( !$hook ) {
 226          $hook = 'ALL';
 227      }
 228      if ( !$sortkey ) {
 229          $sortkey = 'FIRSTNAME';
 230      }
 231      if ( !$sortorder ) {
 232          $sortorder = 'asc';
 233      }
 234  break;
 235  
 236  case 'letter':  /// Looking for entries that begin with a certain letter, ALL or SPECIAL characters
 237  default:
 238      $tab = GLOSSARY_STANDARD_VIEW;
 239      if ( !$hook ) {
 240          $hook = 'ALL';
 241      }
 242  break;
 243  }
 244  
 245  switch ( $tab ) {
 246  case GLOSSARY_IMPORT_VIEW:
 247  case GLOSSARY_EXPORT_VIEW:
 248  case GLOSSARY_APPROVAL_VIEW:
 249      $showcommonelements = 0;
 250  break;
 251  
 252  default:
 253      $showcommonelements = 1;
 254  break;
 255  }
 256  
 257  // Trigger module viewed event.
 258  glossary_view($glossary, $course, $cm, $context, $mode);
 259  
 260  /// Printing the heading
 261  $strglossaries = get_string("modulenameplural", "glossary");
 262  $strglossary = get_string("modulename", "glossary");
 263  $strallcategories = get_string("allcategories", "glossary");
 264  $straddentry = get_string("addentry", "glossary");
 265  $strnoentries = get_string("noentries", "glossary");
 266  $strsearchindefinition = get_string("searchindefinition", "glossary");
 267  $strsearch = get_string("search");
 268  $strwaitingapproval = get_string('waitingapproval', 'glossary');
 269  
 270  /// If we are in approval mode, prit special header
 271  $PAGE->set_title($glossary->name);
 272  $PAGE->set_heading($course->fullname);
 273  $url = new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id));
 274  if (isset($mode)) {
 275      $url->param('mode', $mode);
 276  }
 277  $PAGE->set_url($url);
 278  
 279  if (!empty($CFG->enablerssfeeds) && !empty($CFG->glossary_enablerssfeeds)
 280      && $glossary->rsstype && $glossary->rssarticles) {
 281  
 282      $rsstitle = format_string($course->shortname, true, array('context' => context_course::instance($course->id))) . ': '. format_string($glossary->name);
 283      rss_add_http_header($context, 'mod_glossary', $glossary, $rsstitle);
 284  }
 285  
 286  if ($tab == GLOSSARY_APPROVAL_VIEW) {
 287      require_capability('mod/glossary:approve', $context);
 288      $PAGE->navbar->add($strwaitingapproval);
 289      echo $OUTPUT->header();
 290      echo $OUTPUT->heading($strwaitingapproval);
 291  } else { /// Print standard header
 292      echo $OUTPUT->header();
 293  }
 294  echo $OUTPUT->heading(format_string($glossary->name), 2);
 295  
 296  /// All this depends if whe have $showcommonelements
 297  if ($showcommonelements) {
 298  /// To calculate available options
 299      $availableoptions = '';
 300  
 301  /// Decide about to print the import link
 302      /*if (has_capability('mod/glossary:import', $context)) {
 303          $availableoptions = '<span class="helplink">' .
 304                              '<a href="' . $CFG->wwwroot . '/mod/glossary/import.php?id=' . $cm->id . '"' .
 305                              '  title="' . s(get_string('importentries', 'glossary')) . '">' .
 306                              get_string('importentries', 'glossary') . '</a>' .
 307                              '</span>';
 308      }
 309  /// Decide about to print the export link
 310      if (has_capability('mod/glossary:export', $context)) {
 311          if ($availableoptions) {
 312              $availableoptions .= '&nbsp;/&nbsp;';
 313          }
 314          $availableoptions .='<span class="helplink">' .
 315                              '<a href="' . $CFG->wwwroot . '/mod/glossary/export.php?id=' . $cm->id .
 316                              '&amp;mode='.$mode . '&amp;hook=' . urlencode($hook) . '"' .
 317                              '  title="' . s(get_string('exportentries', 'glossary')) . '">' .
 318                              get_string('exportentries', 'glossary') . '</a>' .
 319                              '</span>';
 320      }*/
 321  
 322  /// Decide about to print the approval link
 323      if (has_capability('mod/glossary:approve', $context)) {
 324      /// Check we have pending entries
 325          if ($hiddenentries = $DB->count_records('glossary_entries', array('glossaryid'=>$glossary->id, 'approved'=>0))) {
 326              if ($availableoptions) {
 327                  $availableoptions .= '<br />';
 328              }
 329              $availableoptions .='<span class="helplink">' .
 330                                  '<a href="' . $CFG->wwwroot . '/mod/glossary/view.php?id=' . $cm->id .
 331                                  '&amp;mode=approval' . '"' .
 332                                  '  title="' . s(get_string('waitingapproval', 'glossary')) . '">' .
 333                                  get_string('waitingapproval', 'glossary') . ' ('.$hiddenentries.')</a>' .
 334                                  '</span>';
 335          }
 336      }
 337  
 338  /// Start to print glossary controls
 339  //        print_box_start('glossarycontrol clearfix');
 340      echo '<div class="glossarycontrol" style="text-align: right">';
 341      echo $availableoptions;
 342  
 343  /// The print icon
 344      if ( $showcommonelements and $mode != 'search') {
 345          if (has_capability('mod/glossary:manageentries', $context) or $glossary->allowprintview) {
 346              echo " <a class='printicon' title =\"". get_string("printerfriendly","glossary") ."\" href=\"print.php?id=$cm->id&amp;mode=$mode&amp;hook=".urlencode($hook)."&amp;sortkey=$sortkey&amp;sortorder=$sortorder&amp;offset=$offset\">" . get_string("printerfriendly","glossary")."</a>";
 347          }
 348      }
 349  /// End glossary controls
 350  //        print_box_end(); /// glossarycontrol
 351      echo '</div><br />';
 352  
 353  //        print_box('&nbsp;', 'clearer');
 354  }
 355  
 356  /// Info box
 357  if ($glossary->intro && $showcommonelements) {
 358      echo $OUTPUT->box(format_module_intro('glossary', $glossary, $cm->id), 'generalbox', 'intro');
 359  }
 360  
 361  /// Search box
 362  if ($showcommonelements ) {
 363      echo '<form method="post" action="view.php">';
 364  
 365      echo '<table class="boxaligncenter" width="70%" border="0">';
 366      echo '<tr><td align="center" class="glossarysearchbox">';
 367  
 368      echo '<input type="submit" value="'.$strsearch.'" name="searchbutton" /> ';
 369      if ($mode == 'search') {
 370          echo '<input type="text" name="hook" size="20" value="'.s($hook).'" alt="'.$strsearch.'" /> ';
 371      } else {
 372          echo '<input type="text" name="hook" size="20" value="" alt="'.$strsearch.'" /> ';
 373      }
 374      if ($fullsearch || $mode != 'search') {
 375          $fullsearchchecked = 'checked="checked"';
 376      } else {
 377          $fullsearchchecked = '';
 378      }
 379      echo '<input type="checkbox" name="fullsearch" id="fullsearch" value="1" '.$fullsearchchecked.' />';
 380      echo '<input type="hidden" name="mode" value="search" />';
 381      echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
 382      echo '<label for="fullsearch">'.$strsearchindefinition.'</label>';
 383      echo '</td></tr></table>';
 384  
 385      echo '</form>';
 386  
 387      echo '<br />';
 388  }
 389  
 390  /// Show the add entry button if allowed
 391  if (has_capability('mod/glossary:write', $context) && $showcommonelements ) {
 392      echo '<div class="singlebutton glossaryaddentry">';
 393      echo "<form id=\"newentryform\" method=\"get\" action=\"$CFG->wwwroot/mod/glossary/edit.php\">";
 394      echo '<div>';
 395      echo "<input type=\"hidden\" name=\"cmid\" value=\"$cm->id\" />";
 396      echo '<input type="submit" value="'.get_string('addentry', 'glossary').'" />';
 397      echo '</div>';
 398      echo '</form>';
 399      echo "</div>\n";
 400  }
 401  
 402  echo '<br />';
 403  
 404  require ("tabs.php");
 405  
 406  require ("sql.php");
 407  
 408  /// printing the entries
 409  $entriesshown = 0;
 410  $currentpivot = '';
 411  $paging = NULL;
 412  
 413  if ($allentries) {
 414  
 415      //Decide if we must show the ALL link in the pagebar
 416      $specialtext = '';
 417      if ($glossary->showall) {
 418          $specialtext = get_string("allentries","glossary");
 419      }
 420  
 421      //Build paging bar
 422      $paging = glossary_get_paging_bar($count, $page, $entriesbypage, "view.php?id=$id&amp;mode=$mode&amp;hook=".urlencode($hook)."&amp;sortkey=$sortkey&amp;sortorder=$sortorder&amp;fullsearch=$fullsearch&amp;",9999,10,'&nbsp;&nbsp;', $specialtext, -1);
 423  
 424      echo '<div class="paging">';
 425      echo $paging;
 426      echo '</div>';
 427  
 428      //load ratings
 429      require_once($CFG->dirroot.'/rating/lib.php');
 430      if ($glossary->assessed != RATING_AGGREGATE_NONE) {
 431          $ratingoptions = new stdClass;
 432          $ratingoptions->context = $context;
 433          $ratingoptions->component = 'mod_glossary';
 434          $ratingoptions->ratingarea = 'entry';
 435          $ratingoptions->items = $allentries;
 436          $ratingoptions->aggregate = $glossary->assessed;//the aggregation method
 437          $ratingoptions->scaleid = $glossary->scale;
 438          $ratingoptions->userid = $USER->id;
 439          $ratingoptions->returnurl = $CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id;
 440          $ratingoptions->assesstimestart = $glossary->assesstimestart;
 441          $ratingoptions->assesstimefinish = $glossary->assesstimefinish;
 442  
 443          $rm = new rating_manager();
 444          $allentries = $rm->get_ratings($ratingoptions);
 445      }
 446  
 447      foreach ($allentries as $entry) {
 448  
 449          // Setting the pivot for the current entry
 450          if ($printpivot) {
 451              $pivot = $entry->{$pivotkey};
 452              $upperpivot = core_text::strtoupper($pivot);
 453              $pivottoshow = core_text::strtoupper(format_string($pivot, true, $fmtoptions));
 454  
 455              // Reduce pivot to 1cc if necessary.
 456              if (!$fullpivot) {
 457                  $upperpivot = core_text::substr($upperpivot, 0, 1);
 458                  $pivottoshow = core_text::substr($pivottoshow, 0, 1);
 459              }
 460  
 461              // If there's a group break.
 462              if ($currentpivot != $upperpivot) {
 463                  $currentpivot = $upperpivot;
 464  
 465                  // print the group break if apply
 466  
 467                  echo '<div>';
 468                  echo '<table cellspacing="0" class="glossarycategoryheader">';
 469  
 470                  echo '<tr>';
 471                  if ($userispivot) {
 472                  // printing the user icon if defined (only when browsing authors)
 473                      echo '<th align="left">';
 474                      $user = mod_glossary_entry_query_builder::get_user_from_record($entry);
 475                      echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
 476                      $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', context_course::instance($course->id)));
 477                  } else {
 478                      echo '<th >';
 479                  }
 480  
 481                  echo $OUTPUT->heading($pivottoshow, 3);
 482                  echo "</th></tr></table></div>\n";
 483              }
 484          }
 485  
 486          /// highlight the term if necessary
 487          if ($mode == 'search') {
 488              //We have to strip any word starting by + and take out words starting by -
 489              //to make highlight works properly
 490              $searchterms = explode(' ', $hook);    // Search for words independently
 491              foreach ($searchterms as $key => $searchterm) {
 492                  if (preg_match('/^\-/',$searchterm)) {
 493                      unset($searchterms[$key]);
 494                  } else {
 495                      $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
 496                  }
 497                  //Avoid highlight of <2 len strings. It's a well known hilight limitation.
 498                  if (strlen($searchterm) < 2) {
 499                      unset($searchterms[$key]);
 500                  }
 501              }
 502              $strippedsearch = implode(' ', $searchterms);    // Rebuild the string
 503              $entry->highlight = $strippedsearch;
 504          }
 505  
 506          /// and finally print the entry.
 507          glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat);
 508          $entriesshown++;
 509      }
 510  }
 511  if ( !$entriesshown ) {
 512      echo $OUTPUT->box(get_string("noentries","glossary"), "generalbox boxaligncenter boxwidthwide");
 513  }
 514  
 515  if (!empty($formsent)) {
 516      // close the form properly if used
 517      echo "</div>";
 518      echo "</form>";
 519  }
 520  
 521  if ( $paging ) {
 522      echo '<hr />';
 523      echo '<div class="paging">';
 524      echo $paging;
 525      echo '</div>';
 526  }
 527  echo '<br />';
 528  glossary_print_tabbed_table_end();
 529  
 530  /// Finish the page
 531  echo $OUTPUT->footer();


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