From c4e868d5f0cbaaada64f3c49c0e419938d80119e Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Sat, 5 Sep 2015 23:09:19 +0800 Subject: [PATCH] MDL-50851 user: use new tag API for interests --- backup/util/dbops/restore_dbops.class.php | 8 ++-- lib/moodlelib.php | 3 +- lib/myprofilelib.php | 12 +++--- tag/user.php | 35 ++++++++--------- user/edit.php | 7 +--- user/editadvanced.php | 7 +--- user/editlib.php | 10 ++--- user/lib.php | 46 +++++++++++++++++++++-- user/profile.php | 1 - user/renderer.php | 34 +++++++++++++++++ user/tests/externallib_test.php | 4 -- user/view.php | 1 - 12 files changed, 110 insertions(+), 58 deletions(-) diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index 880e75dfbfc..b732029eebc 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -1209,16 +1209,14 @@ abstract class restore_dbops { } // Process tags - if (!empty($CFG->usetags) && isset($user->tags)) { // if enabled in server and present in backup + if (core_tag_tag::is_enabled('core', 'user') && isset($user->tags)) { // If enabled in server and present in backup. $tags = array(); foreach($user->tags['tag'] as $usertag) { $usertag = (object)$usertag; $tags[] = $usertag->rawname; } - if (empty($newuserctxid)) { - $newuserctxid = null; // Tag apis expect a null contextid not 0. - } - tag_set('user', $newuserid, $tags, 'core', $newuserctxid); + core_tag_tag::set_item_tags('core', 'user', $newuserid, + context_user::instance($newuserid), $tags); } // Process preferences diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 9b0b8f9ebe7..5638971b966 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3901,7 +3901,6 @@ function delete_user(stdClass $user) { require_once($CFG->libdir.'/grouplib.php'); require_once($CFG->libdir.'/gradelib.php'); require_once($CFG->dirroot.'/message/lib.php'); - require_once($CFG->dirroot.'/tag/lib.php'); require_once($CFG->dirroot.'/user/lib.php'); // Make sure nobody sends bogus record type as parameter. @@ -3944,7 +3943,7 @@ function delete_user(stdClass $user) { // TODO: remove from cohorts using standard API here. // Remove user tags. - tag_set('user', $user->id, array(), 'core', $usercontext->id); + core_tag_tag::remove_all_item_tags('core', 'user', $user->id); // Unconditionally unenrol from all courses. enrol_user_delete($user); diff --git a/lib/myprofilelib.php b/lib/myprofilelib.php index 825753bab22..e397770ad57 100644 --- a/lib/myprofilelib.php +++ b/lib/myprofilelib.php @@ -23,7 +23,6 @@ */ defined('MOODLE_INTERNAL') || die(); -require_once($CFG->dirroot . '/tag/lib.php'); /** * Defines core nodes for my profile navigation tree. @@ -36,7 +35,7 @@ require_once($CFG->dirroot . '/tag/lib.php'); * @return bool */ function core_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) { - global $CFG, $USER, $DB, $PAGE; + global $CFG, $USER, $DB, $PAGE, $OUTPUT; $usercontext = context_user::instance($user->id, MUST_EXIST); $systemcontext = context_system::instance(); @@ -218,11 +217,10 @@ function core_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, } // Printing tagged interests. We want this only for full profile. - if (!empty($CFG->usetags) && empty($course)) { - if ($interests = tag_get_tags_csv('user', $user->id) ) { - $node = new core_user\output\myprofile\node('contact', 'interests', get_string('interests'), null, null, $interests); - $tree->add_node($node); - } + if (empty($course) && ($interests = core_tag_tag::get_item_tags('core', 'user', $user->id))) { + $node = new core_user\output\myprofile\node('contact', 'interests', get_string('interests'), null, null, + $OUTPUT->tag_list($interests, '')); + $tree->add_node($node); } if (!isset($hiddenfields['mycourses'])) { diff --git a/tag/user.php b/tag/user.php index 8b8d7abd932..ffcbfb93a68 100644 --- a/tag/user.php +++ b/tag/user.php @@ -4,8 +4,6 @@ require_once('../config.php'); require_once('lib.php'); $action = optional_param('action', '', PARAM_ALPHA); -$id = optional_param('id', 0, PARAM_INT); -$tag = optional_param('tag', '', PARAM_TAG); require_login(); @@ -25,32 +23,31 @@ $usercontext = context_user::instance($USER->id); switch ($action) { case 'addinterest': - if (empty($tag) && $id) { // for backward-compatibility (people saving bookmarks, mostly..) - $tag = tag_get_name($id); + if (!core_tag_tag::is_enabled('core', 'user')) { + print_error('tagdisabled'); } - - tag_set_add('user', $USER->id, $tag, 'core', $usercontext->id); - - redirect($CFG->wwwroot.'/tag/index.php?tag='. rawurlencode($tag)); + $tag = required_param('tag', PARAM_TAG); + core_tag_tag::add_item_tag('core', 'user', $USER->id, $usercontext, $tag); + $tc = core_tag_area::get_collection('core', 'user'); + redirect(core_tag_tag::make_url($tc, $tag)); break; case 'removeinterest': - if (empty($tag) && $id) { // for backward-compatibility (people saving bookmarks, mostly..) - $tag = tag_get_name($id); + if (!core_tag_tag::is_enabled('core', 'user')) { + print_error('tagdisabled'); } - - tag_set_delete('user', $USER->id, $tag, 'core', $usercontext->id); - - redirect($CFG->wwwroot.'/tag/index.php?tag='. rawurlencode($tag)); + $tag = required_param('tag', PARAM_TAG); + core_tag_tag::remove_item_tag('core', 'user', $USER->id, $tag); + $tc = core_tag_area::get_collection('core', 'user'); + redirect(core_tag_tag::make_url($tc, $tag)); break; case 'flaginappropriate': require_capability('moodle/tag:flag', context_system::instance()); - $tagid = tag_get_id($tag); - - tag_set_flag($tagid); - - redirect($CFG->wwwroot.'/tag/index.php?tag='. rawurlencode($tag), get_string('responsiblewillbenotified', 'tag')); + $id = required_param('id', PARAM_INT); + $tagobject = core_tag_tag::get($id, '*', MUST_EXIST); + $tagobject->flag(); + redirect($tagobject->get_view_url(), get_string('responsiblewillbenotified', 'tag')); break; default: diff --git a/user/edit.php b/user/edit.php index 55e93b92a19..4e141b99ad3 100644 --- a/user/edit.php +++ b/user/edit.php @@ -70,10 +70,7 @@ if (isguestuser($user)) { } // User interests separated by commas. -if (!empty($CFG->usetags)) { - require_once($CFG->dirroot.'/tag/lib.php'); - $user->interests = tag_get_tags_array('user', $user->id); -} +$user->interests = core_tag_tag::get_item_tags_array('core', 'user', $user->id); // Remote users cannot be edited. if (is_mnet_remote_user($user)) { @@ -233,7 +230,7 @@ if ($usernew = $userform->get_data()) { useredit_update_user_preference($usernew); // Update interests. - if (!empty($CFG->usetags)) { + if (isset($usernew->interests)) { useredit_update_interests($usernew, $usernew->interests); } diff --git a/user/editadvanced.php b/user/editadvanced.php index aa74c8d710c..b0aefa6a1ef 100644 --- a/user/editadvanced.php +++ b/user/editadvanced.php @@ -116,10 +116,7 @@ useredit_load_preferences($user); profile_load_data($user); // User interests. -if (!empty($CFG->usetags)) { - require_once($CFG->dirroot.'/tag/lib.php'); - $user->interests = tag_get_tags_array('user', $id); -} +$user->interests = core_tag_tag::get_item_tags_array('core', 'user', $id); if ($user->id !== -1) { $usercontext = context_user::instance($user->id); @@ -236,7 +233,7 @@ if ($usernew = $userform->get_data()) { useredit_update_user_preference($usernew); // Update tags. - if (!empty($CFG->usetags) and empty($USER->newadminuser)) { + if (empty($USER->newadminuser) && isset($usernew->interests)) { useredit_update_interests($usernew, $usernew->interests); } diff --git a/user/editlib.php b/user/editlib.php index 509a06dfe7f..ae9504d840b 100644 --- a/user/editlib.php +++ b/user/editlib.php @@ -264,9 +264,8 @@ function useredit_update_trackforums($user, $usernew) { * @param array $interests */ function useredit_update_interests($user, $interests) { - global $CFG; - require_once($CFG->dirroot . '/tag/lib.php'); - tag_set('user', $user->id, $interests, 'core', context_user::instance($user->id)->id); + core_tag_tag::set_item_tags('core', 'user', $user->id, + context_user::instance($user->id), $interests); } /** @@ -397,9 +396,10 @@ function useredit_shared_definition(&$mform, $editoroptions, $filemanageroptions } } - if (!empty($CFG->usetags) and empty($USER->newadminuser)) { + if (core_tag_tag::is_enabled('core', 'user') and empty($USER->newadminuser)) { $mform->addElement('header', 'moodle_interests', get_string('interests')); - $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial')); + $mform->addElement('tags', 'interests', get_string('interestslist'), + array('display' => 'noofficial', 'itemtype' => 'user', 'component' => 'core')); $mform->addHelpButton('interests', 'interestslist'); } diff --git a/user/lib.php b/user/lib.php index 243db86c702..836562414d5 100644 --- a/user/lib.php +++ b/user/lib.php @@ -463,10 +463,10 @@ function user_get_user_details($user, $course = null, array $userfields = array( $userdetails['email'] = $user->email; } - if (in_array('interests', $userfields) && !empty($CFG->usetags)) { - require_once($CFG->dirroot . '/tag/lib.php'); - if ($interests = tag_get_tags_csv('user', $user->id, TAG_RETURN_TEXT) ) { - $userdetails['interests'] = $interests; + if (in_array('interests', $userfields)) { + $interests = core_tag_tag::get_item_tags_array('core', 'user', $user->id, null, 0, false); + if ($interests) { + $userdetails['interests'] = join(', ', $interests); } } @@ -1134,3 +1134,41 @@ function user_can_view_profile($user, $course = null, $usercontext = null) { } return false; } + +/** + * Returns users 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 user_get_tagged_users($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = 1, $page = 0) { + global $PAGE; + + if ($ctx && $ctx != context_system::instance()->id) { + $usercount = 0; + } else { + // Users can only be displayed in system context. + $usercount = $tag->count_tagged_items('core', 'user', + 'it.deleted=:notdeleted', array('notdeleted' => 0)); + } + $perpage = $exclusivemode ? 24 : 5; + $content = ''; + $totalpages = ceil($usercount / $perpage); + + if ($usercount) { + $userlist = $tag->get_tagged_items('core', 'user', $page * $perpage, $perpage, + 'it.deleted=:notdeleted', array('notdeleted' => 0)); + $renderer = $PAGE->get_renderer('core', 'user'); + $content .= $renderer->user_list($userlist, $exclusivemode); + } + + return new core_tag\output\tagindex($tag, 'core', 'user', $content, + $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages); +} diff --git a/user/profile.php b/user/profile.php index cb7f7fe5db9..b14f81bffe9 100644 --- a/user/profile.php +++ b/user/profile.php @@ -34,7 +34,6 @@ require_once(dirname(__FILE__) . '/../config.php'); require_once($CFG->dirroot . '/my/lib.php'); -require_once($CFG->dirroot . '/tag/lib.php'); require_once($CFG->dirroot . '/user/profile/lib.php'); require_once($CFG->dirroot . '/user/lib.php'); require_once($CFG->libdir.'/filelib.php'); diff --git a/user/renderer.php b/user/renderer.php index bd14e9819c8..c12f579d266 100644 --- a/user/renderer.php +++ b/user/renderer.php @@ -164,6 +164,40 @@ class core_user_renderer extends plugin_renderer_base { return $content; } + /** + * 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 = '
\n"; + return $output; + } + + return $this->output->render_from_template('core_tag/tagfeed', $items); + } + } /** diff --git a/user/tests/externallib_test.php b/user/tests/externallib_test.php index 186ae108522..ebbb290089a 100644 --- a/user/tests/externallib_test.php +++ b/user/tests/externallib_test.php @@ -70,7 +70,6 @@ class core_user_externallib_testcase extends externallib_advanced_testcase { $user1 = self::getDataGenerator()->create_user($user1); set_config('usetags', 1); require_once($CFG->dirroot . '/user/editlib.php'); - require_once($CFG->dirroot . '/tag/lib.php'); $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking'); useredit_update_interests($user1, $user1->interests); @@ -228,7 +227,6 @@ class core_user_externallib_testcase extends externallib_advanced_testcase { $user1 = self::getDataGenerator()->create_user($user1); if (!empty($CFG->usetags)) { require_once($CFG->dirroot . '/user/editlib.php'); - require_once($CFG->dirroot . '/tag/lib.php'); $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking'); useredit_update_interests($user1, $user1->interests); } @@ -390,7 +388,6 @@ class core_user_externallib_testcase extends externallib_advanced_testcase { $return->user1 = self::getDataGenerator()->create_user($return->user1); if (!empty($CFG->usetags)) { require_once($CFG->dirroot . '/user/editlib.php'); - require_once($CFG->dirroot . '/tag/lib.php'); $return->user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking'); useredit_update_interests($return->user1, $return->user1->interests); } @@ -594,7 +591,6 @@ class core_user_externallib_testcase extends externallib_advanced_testcase { $user1 = self::getDataGenerator()->create_user($user1); if (!empty($CFG->usetags)) { require_once($CFG->dirroot . '/user/editlib.php'); - require_once($CFG->dirroot . '/tag/lib.php'); $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking'); useredit_update_interests($user1, $user1->interests); } diff --git a/user/view.php b/user/view.php index 5040d088d4f..8fbca1412e5 100644 --- a/user/view.php +++ b/user/view.php @@ -25,7 +25,6 @@ require_once("../config.php"); require_once($CFG->dirroot.'/user/profile/lib.php'); require_once($CFG->dirroot.'/user/lib.php'); -require_once($CFG->dirroot.'/tag/lib.php'); require_once($CFG->libdir . '/filelib.php'); require_once($CFG->libdir . '/badgeslib.php');