PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/filter/activitynames/filter.php

https://github.com/henriquecrang/e-UNI
PHP | 85 lines | 55 code | 18 blank | 12 comment | 18 complexity | 4e49a6adbdc80148ce63ce027cf2914f MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, BSD-3-Clause
  1. <?php // $Id: filter.php,v 1.21.2.2 2008/12/07 18:55:18 stronk7 Exp $
  2. //This function provides automatic linking to
  3. //activities when its name (title) is found inside every Moodle text
  4. //It's based in the glosssary filter by Williams Castillo
  5. //Modifications by stronk7.
  6. function activitynames_filter($courseid, $text) {
  7. global $CFG, $COURSE;
  8. // Trivial-cache - keyed on $cachedcourseid
  9. static $activitylist = null;
  10. static $cachedcourseid;
  11. if (empty($courseid)) {
  12. $courseid = SITEID;
  13. }
  14. // Initialise/invalidate our trivial cache if dealing with a different course
  15. if (!isset($cachedcourseid) || $cachedcourseid !== (int)$courseid) {
  16. $activitylist = null;
  17. }
  18. $cachedcourseid = (int)$courseid;
  19. /// It may be cached
  20. if (is_null($activitylist)) {
  21. $activitylist = array();
  22. if ($COURSE->id == $courseid) {
  23. $course = $COURSE;
  24. } else {
  25. $course = get_record("course", "id", $courseid);
  26. }
  27. if (!isset($course->modinfo)) {
  28. return $text;
  29. }
  30. /// Casting $course->modinfo to string prevents one notice when the field is null
  31. $modinfo = unserialize((string)$course->modinfo);
  32. if (!empty($modinfo)) {
  33. $activitylist = array(); /// We will store all the activities here
  34. //Sort modinfo by name length
  35. usort($modinfo, 'comparemodulenamesbylength');
  36. foreach ($modinfo as $activity) {
  37. //Exclude labels, hidden activities and activities for group members only
  38. if ($activity->mod != "label" and $activity->visible and empty($activity->groupmembersonly)) {
  39. $title = s(trim(strip_tags(urldecode($activity->name))));
  40. $currentname = trim(urldecode($activity->name));
  41. $entitisedname = s($currentname);
  42. /// Avoid empty or unlinkable activity names
  43. if (!empty($title)) {
  44. $href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\" $CFG->frametarget>";
  45. $activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
  46. if ($currentname != $entitisedname) { /// If name has some entity (&amp; &quot; &lt; &gt;) add that filter too. MDL-17545
  47. $activitylist[] = new filterobject($entitisedname, $href_tag_begin, '</a>', false, true);
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. if ($activitylist) {
  55. return $text = filter_phrases ($text, $activitylist);
  56. } else {
  57. return $text;
  58. }
  59. }
  60. //This function is used to order module names from longer to shorter
  61. function comparemodulenamesbylength($a, $b) {
  62. if (strlen($a->name) == strlen($b->name)) {
  63. return 0;
  64. }
  65. return (strlen($a->name) < strlen($b->name)) ? 1 : -1;
  66. }
  67. ?>