示例#1
0
 /**
  * Renders html to print list of courses tagged with particular tag
  *
  * @param int $tagid id of the tag
  * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
  *             are displayed on the page and the per-page limit may be bigger
  * @param int $fromctx context id where the link was displayed, may be used by callbacks
  *            to display items in the same context first
  * @param int $ctx context id where to search for records
  * @param bool $rec search in subcontexts as well
  * @param array $displayoptions
  * @return string empty string if no courses are marked with this tag or rendered list of courses
  */
 public function tagged_courses($tagid, $exclusivemode = true, $ctx = 0, $rec = true, $displayoptions = null)
 {
     global $CFG;
     require_once $CFG->libdir . '/coursecatlib.php';
     if (empty($displayoptions)) {
         $displayoptions = array();
     }
     $showcategories = coursecat::count_all() > 1;
     $displayoptions += array('limit' => $CFG->coursesperpage, 'offset' => 0);
     $chelper = new coursecat_helper();
     $searchcriteria = array('tagid' => $tagid, 'ctx' => $ctx, 'rec' => $rec);
     $chelper->set_show_courses($showcategories ? self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT : self::COURSECAT_SHOW_COURSES_EXPANDED)->set_search_criteria($searchcriteria)->set_courses_display_options($displayoptions)->set_attributes(array('class' => 'course-search-result course-search-result-tagid'));
     // (we set the same css class as in search results by tagid)
     if ($totalcount = coursecat::search_courses_count($searchcriteria)) {
         $courses = coursecat::search_courses($searchcriteria, $chelper->get_courses_display_options());
         if ($exclusivemode) {
             return $this->coursecat_courses($chelper, $courses, $totalcount);
         } else {
             $tagfeed = new core_tag\output\tagfeed();
             $img = $this->output->pix_icon('i/course', '');
             foreach ($courses as $course) {
                 $url = course_get_url($course);
                 $imgwithlink = html_writer::link($url, $img);
                 $coursename = html_writer::link($url, $course->get_formatted_name());
                 $details = '';
                 if ($showcategories && ($cat = coursecat::get($course->category, IGNORE_MISSING))) {
                     $details = get_string('category') . ': ' . html_writer::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id)), $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed'));
                 }
                 $tagfeed->add($imgwithlink, $coursename, $details);
             }
             return $this->output->render_from_template('core_tag/tagfeed', $tagfeed->export_for_template($this->output));
         }
     }
     return '';
 }
示例#2
0
文件: lib.php 项目: janeklb/moodle
/**
 * Returns course modules tagged with a specified tag ready for output on tag/index.php page
 *
 * This is a callback used by the tag area core/course_modules to search for course modules
 * tagged with a specific tag.
 *
 * @param core_tag_tag $tag
 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
 *             are displayed on the page and the per-page limit may be bigger
 * @param int $fromcontextid context id where the link was displayed, may be used by callbacks
 *            to display items in the same context first
 * @param int $contextid context id where to search for records
 * @param bool $recursivecontext search in subcontexts as well
 * @param int $page 0-based number of page being displayed
 * @return \core_tag\output\tagindex
 */
function course_get_tagged_course_modules($tag, $exclusivemode = false, $fromcontextid = 0, $contextid = 0, $recursivecontext = 1, $page = 0)
{
    global $OUTPUT;
    $perpage = $exclusivemode ? 20 : 5;
    // Build select query.
    $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
    $query = "SELECT cm.id AS cmid, c.id AS courseid, {$ctxselect}\n                FROM {course_modules} cm\n                JOIN {tag_instance} tt ON cm.id = tt.itemid\n                JOIN {course} c ON cm.course = c.id\n                JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :coursemodulecontextlevel\n               WHERE tt.itemtype = :itemtype AND tt.tagid = :tagid AND tt.component = :component\n                AND c.id %COURSEFILTER% AND cm.id %ITEMFILTER%";
    $params = array('itemtype' => 'course_modules', 'tagid' => $tag->id, 'component' => 'core', 'coursemodulecontextlevel' => CONTEXT_MODULE);
    if ($contextid) {
        $context = context::instance_by_id($contextid);
        $query .= $recursivecontext ? ' AND (ctx.id = :contextid OR ctx.path LIKE :path)' : ' AND ctx.id = :contextid';
        $params['contextid'] = $context->id;
        $params['path'] = $context->path . '/%';
    }
    $query .= ' ORDER BY';
    if ($fromcontextid) {
        // In order-clause specify that modules from inside "fromctx" context should be returned first.
        $fromcontext = context::instance_by_id($fromcontextid);
        $query .= ' (CASE WHEN ctx.id = :fromcontextid OR ctx.path LIKE :frompath THEN 0 ELSE 1 END),';
        $params['fromcontextid'] = $fromcontext->id;
        $params['frompath'] = $fromcontext->path . '/%';
    }
    $query .= ' c.sortorder, cm.id';
    $totalpages = $page + 1;
    // Use core_tag_index_builder to build and filter the list of items.
    // Request one item more than we need so we know if next page exists.
    $builder = new core_tag_index_builder('core', 'course_modules', $query, $params, $page * $perpage, $perpage + 1);
    while ($item = $builder->has_item_that_needs_access_check()) {
        context_helper::preload_from_record($item);
        $courseid = $item->courseid;
        if (!$builder->can_access_course($courseid)) {
            $builder->set_accessible($item, false);
            continue;
        }
        $modinfo = get_fast_modinfo($builder->get_course($courseid));
        // Set accessibility of this item and all other items in the same course.
        $builder->walk(function ($taggeditem) use($courseid, $modinfo, $builder) {
            if ($taggeditem->courseid == $courseid) {
                $cm = $modinfo->get_cm($taggeditem->cmid);
                $builder->set_accessible($taggeditem, $cm->uservisible);
            }
        });
    }
    $items = $builder->get_items();
    if (count($items) > $perpage) {
        $totalpages = $page + 2;
        // We don't need exact page count, just indicate that the next page exists.
        array_pop($items);
    }
    // Build the display contents.
    if ($items) {
        $tagfeed = new core_tag\output\tagfeed();
        foreach ($items as $item) {
            context_helper::preload_from_record($item);
            $course = $builder->get_course($item->courseid);
            $modinfo = get_fast_modinfo($course);
            $cm = $modinfo->get_cm($item->cmid);
            $courseurl = course_get_url($item->courseid, $cm->sectionnum);
            $cmname = $cm->get_formatted_name();
            if (!$exclusivemode) {
                $cmname = shorten_text($cmname, 100);
            }
            $cmname = html_writer::link($cm->url ?: $courseurl, $cmname);
            $coursename = format_string($course->fullname, true, array('context' => context_course::instance($item->courseid)));
            $coursename = html_writer::link($courseurl, $coursename);
            $icon = html_writer::empty_tag('img', array('src' => $cm->get_icon_url()));
            $tagfeed->add($icon, $cmname, $coursename);
        }
        $content = $OUTPUT->render_from_template('core_tag/tagfeed', $tagfeed->export_for_template($OUTPUT));
        return new core_tag\output\tagindex($tag, 'core', 'course_modules', $content, $exclusivemode, $fromcontextid, $contextid, $recursivecontext, $page, $totalpages);
    }
}
示例#3
0
/**
 * Returns wiki pages tagged with a specified tag.
 *
 * This is a callback used by the tag area mod_wiki/wiki_pages to search for wiki pages
 * tagged with a specific tag.
 *
 * @param core_tag_tag $tag
 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
 *             are displayed on the page and the per-page limit may be bigger
 * @param int $fromctx context id where the link was displayed, may be used by callbacks
 *            to display items in the same context first
 * @param int $ctx context id where to search for records
 * @param bool $rec search in subcontexts as well
 * @param int $page 0-based number of page being displayed
 * @return \core_tag\output\tagindex
 */
function mod_wiki_get_tagged_pages($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = 1, $page = 0)
{
    global $OUTPUT;
    $perpage = $exclusivemode ? 20 : 5;
    // Build the SQL query.
    $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
    $query = "SELECT wp.id, wp.title, ws.userid, ws.wikiid, ws.id AS subwikiid, ws.groupid, w.wikimode,\n                    cm.id AS cmid, c.id AS courseid, c.shortname, c.fullname, {$ctxselect}\n                FROM {wiki_pages} wp\n                JOIN {wiki_subwikis} ws ON wp.subwikiid = ws.id\n                JOIN {wiki} w ON w.id = ws.wikiid\n                JOIN {modules} m ON m.name='wiki'\n                JOIN {course_modules} cm ON cm.module = m.id AND cm.instance = w.id\n                JOIN {tag_instance} tt ON wp.id = tt.itemid\n                JOIN {course} c ON cm.course = c.id\n                JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :coursemodulecontextlevel\n               WHERE tt.itemtype = :itemtype AND tt.tagid = :tagid AND tt.component = :component\n                 AND wp.id %ITEMFILTER% AND c.id %COURSEFILTER%";
    $params = array('itemtype' => 'wiki_pages', 'tagid' => $tag->id, 'component' => 'mod_wiki', 'coursemodulecontextlevel' => CONTEXT_MODULE);
    if ($ctx) {
        $context = $ctx ? context::instance_by_id($ctx) : context_system::instance();
        $query .= $rec ? ' AND (ctx.id = :contextid OR ctx.path LIKE :path)' : ' AND ctx.id = :contextid';
        $params['contextid'] = $context->id;
        $params['path'] = $context->path . '/%';
    }
    $query .= " ORDER BY ";
    if ($fromctx) {
        // In order-clause specify that modules from inside "fromctx" context should be returned first.
        $fromcontext = context::instance_by_id($fromctx);
        $query .= ' (CASE WHEN ctx.id = :fromcontextid OR ctx.path LIKE :frompath THEN 0 ELSE 1 END),';
        $params['fromcontextid'] = $fromcontext->id;
        $params['frompath'] = $fromcontext->path . '/%';
    }
    $query .= ' c.sortorder, cm.id, wp.id';
    $totalpages = $page + 1;
    // Use core_tag_index_builder to build and filter the list of items.
    $builder = new core_tag_index_builder('mod_wiki', 'wiki_pages', $query, $params, $page * $perpage, $perpage + 1);
    while ($item = $builder->has_item_that_needs_access_check()) {
        context_helper::preload_from_record($item);
        $courseid = $item->courseid;
        if (!$builder->can_access_course($courseid)) {
            $builder->set_accessible($item, false);
            continue;
        }
        $modinfo = get_fast_modinfo($builder->get_course($courseid));
        // Set accessibility of this item and all other items in the same course.
        $builder->walk(function ($taggeditem) use($courseid, $modinfo, $builder) {
            if ($taggeditem->courseid == $courseid) {
                $accessible = false;
                if (($cm = $modinfo->get_cm($taggeditem->cmid)) && $cm->uservisible) {
                    $subwiki = (object) array('id' => $taggeditem->subwikiid, 'groupid' => $taggeditem->groupid, 'userid' => $taggeditem->userid, 'wikiid' => $taggeditem->wikiid);
                    $wiki = (object) array('id' => $taggeditem->wikiid, 'wikimode' => $taggeditem->wikimode, 'course' => $cm->course);
                    $accessible = wiki_user_can_view($subwiki, $wiki);
                }
                $builder->set_accessible($taggeditem, $accessible);
            }
        });
    }
    $items = $builder->get_items();
    if (count($items) > $perpage) {
        $totalpages = $page + 2;
        // We don't need exact page count, just indicate that the next page exists.
        array_pop($items);
    }
    // Build the display contents.
    if ($items) {
        $tagfeed = new core_tag\output\tagfeed();
        foreach ($items as $item) {
            context_helper::preload_from_record($item);
            $modinfo = get_fast_modinfo($item->courseid);
            $cm = $modinfo->get_cm($item->cmid);
            $pageurl = new moodle_url('/mod/wiki/view.php', array('pageid' => $item->id));
            $pagename = format_string($item->title, true, array('context' => context_module::instance($item->cmid)));
            $pagename = html_writer::link($pageurl, $pagename);
            $courseurl = course_get_url($item->courseid, $cm->sectionnum);
            $cmname = html_writer::link($cm->url, $cm->get_formatted_name());
            $coursename = format_string($item->fullname, true, array('context' => context_course::instance($item->courseid)));
            $coursename = html_writer::link($courseurl, $coursename);
            $icon = html_writer::link($pageurl, html_writer::empty_tag('img', array('src' => $cm->get_icon_url())));
            $tagfeed->add($icon, $pagename, $cmname . '<br>' . $coursename);
        }
        $content = $OUTPUT->render_from_template('core_tag/tagfeed', $tagfeed->export_for_template($OUTPUT));
        return new core_tag\output\tagindex($tag, 'mod_wiki', 'wiki_pages', $content, $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
    }
}
示例#4
0
 /**
  * Displays the list of tagged users
  *
  * @param array $userlist
  * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
  *             are displayed on the page and the per-page limit may be bigger
  * @return string
  */
 public function user_list($userlist, $exclusivemode)
 {
     $tagfeed = new core_tag\output\tagfeed();
     foreach ($userlist as $user) {
         $userpicture = $this->output->user_picture($user, array('size' => $exclusivemode ? 100 : 35));
         $fullname = fullname($user);
         if (user_can_view_profile($user)) {
             $profilelink = new moodle_url('/user/view.php', array('id' => $user->id));
             $fullname = html_writer::link($profilelink, $fullname);
         }
         $tagfeed->add($userpicture, $fullname);
     }
     $items = $tagfeed->export_for_template($this->output);
     if ($exclusivemode) {
         $output = '<div><ul class="inline-list">';
         foreach ($items['items'] as $item) {
             $output .= '<li><div class="user-box">' . $item['img'] . $item['heading'] . "</div></li>\n";
         }
         $output .= "</ul></div>\n";
         return $output;
     }
     return $this->output->render_from_template('core_tag/tagfeed', $items);
 }
示例#5
0
文件: lib.php 项目: evltuma/moodle
/**
 * Returns posts tagged with a specified tag.
 *
 * @param core_tag_tag $tag
 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
 *             are displayed on the page and the per-page limit may be bigger
 * @param int $fromctx context id where the link was displayed, may be used by callbacks
 *            to display items in the same context first
 * @param int $ctx context id where to search for records
 * @param bool $rec search in subcontexts as well
 * @param int $page 0-based number of page being displayed
 * @return \core_tag\output\tagindex
 */
function blog_get_tagged_posts($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = true, $page = 0)
{
    global $CFG, $OUTPUT;
    require_once $CFG->dirroot . '/user/lib.php';
    $systemcontext = context_system::instance();
    $perpage = $exclusivemode ? 20 : 5;
    $context = $ctx ? context::instance_by_id($ctx) : context_system::instance();
    $content = '';
    if (empty($CFG->enableblogs) || !has_capability('moodle/blog:view', $systemcontext)) {
        // Blogs are not enabled or are not visible to the current user.
        $totalpages = 0;
    } else {
        if ($context->contextlevel != CONTEXT_SYSTEM && empty($CFG->useblogassociations)) {
            // No blog entries can be associated to the non-system context.
            $totalpages = 0;
        } else {
            if (!$rec && $context->contextlevel != CONTEXT_COURSE && $context->contextlevel != CONTEXT_MODULE) {
                // No blog entries can be associated with category or block context.
                $totalpages = 0;
            } else {
                require_once $CFG->dirroot . '/blog/locallib.php';
                $filters = array('tag' => $tag->id);
                if ($rec) {
                    if ($context->contextlevel != CONTEXT_SYSTEM) {
                        $filters['context'] = $context->id;
                    }
                } else {
                    if ($context->contextlevel == CONTEXT_COURSE) {
                        $filters['course'] = $context->instanceid;
                    } else {
                        if ($context->contextlevel == CONTEXT_MODULE) {
                            $filters['module'] = $context->instanceid;
                        }
                    }
                }
                $bloglisting = new blog_listing($filters);
                $blogs = $bloglisting->get_entries($page * $perpage, $perpage);
                $totalcount = $bloglisting->count_entries();
                $totalpages = ceil($totalcount / $perpage);
                if (!empty($blogs)) {
                    $tagfeed = new core_tag\output\tagfeed();
                    foreach ($blogs as $blog) {
                        $user = fullclone($blog);
                        $user->id = $blog->userid;
                        $user->deleted = 0;
                        $img = $OUTPUT->user_picture($user, array('size' => 35));
                        $subject = format_string($blog->subject);
                        if ($blog->publishstate == 'draft') {
                            $class = 'dimmed';
                        } else {
                            $class = '';
                        }
                        $url = new moodle_url('/blog/index.php', array('entryid' => $blog->id));
                        $subject = html_writer::link($url, $subject, array('class' => $class));
                        $fullname = fullname($user);
                        if (user_can_view_profile($user)) {
                            $profilelink = new moodle_url('/user/view.php', array('id' => $blog->userid));
                            $fullname = html_writer::link($profilelink, $fullname);
                        }
                        $details = $fullname . ', ' . userdate($blog->created);
                        $tagfeed->add($img, $subject, $details);
                    }
                    $items = $tagfeed->export_for_template($OUTPUT);
                    $content = $OUTPUT->render_from_template('core_tag/tagfeed', $items);
                    $urlparams = array('tagid' => $tag->id);
                    if ($context->contextlevel == CONTEXT_COURSE) {
                        $urlparams['courseid'] = $context->instanceid;
                    } else {
                        if ($context->contextlevel == CONTEXT_MODULE) {
                            $urlparams['modid'] = $context->instanceid;
                        }
                    }
                    $allblogsurl = new moodle_url('/blog/index.php', $urlparams);
                    $rv = new core_tag\output\tagindex($tag, 'core', 'post', $content, $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
                    $rv->exclusiveurl = $allblogsurl;
                    return $rv;
                }
            }
        }
    }
    $rv = new core_tag\output\tagindex($tag, 'core', 'post', $content, $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
    $rv->exclusiveurl = null;
    return $rv;
}