what function could use to set course teacher
I used this to get course students or teachers
$context = get_context_instance(CONTEXT_COURSE, course_id);
$students = get_role_users(5, $context);
but what in need is to set course teacher by a function
Assign a user a teacher role on a course
$coursecontext = context_course::instance($courseid);
$teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
role_assign($teacherroleid, $userid, $coursecontext);
UPDATE: manually enrol a user on a course - this will also assign a role so you might not need the above code?
if (enrol_is_enabled('manual')) {
// Ensure the manual enrolment plugin is enabled.
$enrolplugin = enrol_get_plugin('manual');
// Lookup the manual enrolment instance for this course.
$instances = enrol_get_instances($this->course->id, true);
foreach ($instances as $instance) {
if ($instance->enrol === 'manual') {
break;
}
}
if ($instance->enrol !== 'manual') {
throw new coding_exception('No manual enrol plugin in course');
}
// Enrol the user with the required role
$enrolplugin->enrol_user($instance, $userid, $roleid);
}
Related
I need help. I only need to be able to view one user for a post in OctoberCMS.
But all users will always show up to me.
Here's the code that works, but I don't know how to define it to work the way I need it.
idea: https://ibb.co/VM2Rrr3
public function getUserOptions()
{
$result = [0 => 'indikator.news::lang.form.select_user'];
$users = Db::table('backend_users')->orderBy('login', 'asc')->get()->all();
foreach ($users as $user) {
$name = trim($user->first_name.' '.$user->last_name);
$name = ($name != '') ? ' ('.$name.')' : '';
$result[$user->id] = $user->login.$name;
}
return $result;
}
Use this as user who is creating project is user who is currently logged in so we can use current logged-in user that.
use BackendAuth;
public function getUserOptions()
{
// placeholder
$result = [0 => 'indikator.news::lang.form.select_user'];
// only back-end user which is currently logged in
// it will be available always as we are in back-end :)
$user = BackendAuth::getUser();
$result[$user->id] = $user->login; // or $user->email;
return $result;
}
It will get logged in user and show in drop-down for selection [ or even you skip place-holder to have only 1 selected option]
if any doubts please comment.
Inside the $get_user and $get_code queries they both have a group_id.
I have dd(); them Both and made 100% sure.
the $get_user query has multiple group_id's and the $get_code only has one group_id which is equal to one of the $get_user group_id's.
The goal at the moment is to create a group_id match query.
Get the code that has a group ID equal to one of the $get_user group_id's
public function getCodesViewQr($code_id)
{
$userid = Auth::id();
$get_user = GroupUser::all()->where('user_id',$userid);
$get_code = Code::all()->where('id',$code_id);
$group_match = GroupUser::where('group_id', $get_code->group_id);
$view['get_users'] = $get_user;
$view['get_codes'] = $get_code;
$view['group_matchs'] = $group_match;
return view('codes.view_qr_code', $view);
}
The group match query does not work. $get_code->group_id does not get the code group_id.
If there is a match then set $match equal to rue. else $match is False
$group_match = GroupUser::where('group_id', $get_code->group_id);
I'm using two Models Code and GroupUser
My Code table is like this :
-id
-group_id (This is the only on important right now)
-code_type
My GroupUser table is like this :
-id
-group_id (This is the only on important right now)
-user_id
-user_role
I have linked the Models
Inside my Code Model I have the relationship to GroupUser
public function group_user()
{
return $this->belongsto('App\GroupUser');
}
And Inside my GroupUser Model I have the relationship to Code
public function code()
{
return $this->belongsto('App\Code');
}
Inside My Code controller I have included my models.
use App\Code;
use App\GroupUser;
Hi guys so I had some help from a guy I work with and this is the solution he came up with. We made a few adjustments. all the Databases and results stayed the same. we just changed the method we used to get the results.
I really appreciate all the help from #linktoahref
public function view_code($random)
{
$code = Code::where('random', $random)->first();
$view['code'] = $code;
if ($code->code_type == 1)
{
// Its a coupon
if (!empty(Auth::user()))
{
// Someones is logged in
$user = Auth::user();
$view['user'] = $user;
$user_groups = GroupUser::where('user_id',$user->id)->pluck('group_id')->toArray();
if (in_array($code->group_id, $user_groups))
{
// The user is an admin of this code
return view('view_codes.coupon_admin', $view);
}else
{
// Save the code to that users account
return view('view_codes.generic_code', $view);
}
}else
{
// Anon
return view('view_codes.coupon_anon', $view);
}
}elseif ($code->code_type == 2)
{
// Voucher..
}else
{
// We don't know how to deal with that code type
}
}
$get_code = Code::find($code_id);
// Check if the code isn't null, else give a fallback to group_id
$group_id = 0;
if (! is_null($get_code)) {
$group_id = $get_code->group_id;
}
$group_match = GroupUser::where('group_id', $group_id)
->get();
$match = FALSE;
if ($group_match->count()) {
$match = TRUE;
}
So here's what I'm trying to do. I'm working on a plugin and I want to check each comment and see if the author of that comment has a role of "administrator" or "editor". If they do, rather than display their user name and avatar, I would like to display the name of the website along with a company logo or something. I'm pretty new to WordPress development and am stuck on this. I can't figure out if there is a filter for this, or if I need to create a custom comments template. If someone could even just get me pointed in the right direction that would be great, because at this point, i'm not even sure where I should start. Thank you.
Where I am, My thought process:
<?php
function anonymize_author(){
global $post;
//get the id of the comment author
$author_id = $post->post_author;
//get the userdata of comment author
$author_info = get_userdata($author_id);
//get the user roles of comment author
$author_roles = $author_info->roles;
//Array of roles to check against
$roles_to_check = ["editor", "administrator"];
//see if user has a role in my $roles_to_check array
$results = array_intersect($roles_to_check, $author_roles);
if(!empty($results)){
//the user has roles of either "editor" or "administrator"
//load custom comments page?
//I need to display the author name as the site name
//and the avatar as the site logo
}else{
//Just a regular user, load the Wordpress Default comments
}
}
add_filter('some_filter_here', 'anonymize_author');
When displaying the comments, check to see if comment name is "admin", something like this:
// check to see if the comment author name is admin
if ( "admin" == get_comment_author() ) {
// your condition
}
for this to work, when commenting as site administrator, you should use the name admin. You can also use get_comment_author_email() to match for admin email:
$all_users = get_users(); // get all users
// loop through all users
foreach ($all_users as $user) {
// if the user email matches the commenter's email
// and user has admin role
if ( $user->user_email == get_comment_author_email() && in_array( 'administrator', (array) $user->roles ))
{
// your condition
}
}
Using a filter was the way to go. I found two hooks that fit my need:
get_comment_author_link and get_avatar_url.
Here is how I applied them to my problem:
//plugin.php
/*
* A function that compares users roles
*/
function check_user_roles($user_roles){
//array of roles to check
$roles_to_check = ["editor", "administrator"];
//see if user roles match any $roles_to_check values
$results = array_intersect($roles_to_check, (array) $user_roles);
if(!empty($results)){
return true;
}else{
return false;
}
}
/*
* Callback function for the get_comment_author_link filter
*/
function anonymize_author($return, $author, $comment_id){
//find comment data
$comment_data = get_comment($comment_id);
//find authors user_id
$user_id = $comment_data->user_id;
//check that author is a registered user before proceeding
if($user_id > 0){
//get user data
$user = get_user_by('id', $user_id);
//get users roles
$user_roles = $user->roles;
if(check_user_roles($user_roles)){
$author = bloginfo('name');
}
}
return $author;
}
/*
* Callback function for the get_avatar_url filter
*/
function anonymizer_avatar($url, $id_or_email, $args){
$user = false;
if(is_numeric($id_or_email)){
$id = (int) $id_or_email;
$user = get_user_by('id', $id);
}elseif(is_object($id_or_email)){
if(!empty($id_or_email->user_id)){
$id = (int) $id_or_email->user_id;
$user = get_user_by('id', $id);
}
}else{
$user = get_user_by('email', $id_or_email);
}
if($user && is_object($user)){
$user_roles = $user->roles;
if(check_user_roles($user_roles)){
$url = "URL_TO_MY_IMAGE";
}
}
return $url;
}
add_filter('get_avatar_url', 'anonymize_avatar', 1, 3);
add_filter('get_comment_author_link', 'anonymize_author', 1, 3);
I need to loop all Moodle registered users and get their usernames, in order to compare their values with the data I extracted from https://clip.unl.pt/sprs?lg=pt&year=2013&uo=97747&srv=rsu&p=1&tp=s&md=3&rs=8145&it=1030123459
The data that I want is the values inside Identificador.
The Requires:
require_once('../../config.php');
require_once('../../lib/accesslib.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/user/lib.php');
require_once('lib.php');
require_login();
if (!isset($SESSION->bulk_users)) {
$SESSION->bulk_users = array();
}
function buildURL($year, $period, $typeperiod,$course)
{
return 'https://clip.unl.pt/sprs?lg=pt&year='.$year.'&uo=97747&srv=rsu&p='.$period.'&tp='.$typeperiod.'&md=3&rs='.$course.'&it=1030123459';
}
function doRequest_with_FileGetContents($url)
{
return file_get_contents($url);
}
This is getting me the values inside Identificador tags
function processXML($xmlContent){
$xmlObj= new SimpleXMLElement($xmlContent);
foreach($xmlObj->unidade_curricular->inscritos->aluno as $aluno){
$result= $aluno->identificador."<br/>";
return $result;
}
This gets me all Moodle users
function getallUsers(){
global $DB;
$users= $DB->get_records('user');
foreach($users as $user){
$allusers= $user->username."<br/>";
return $allusers;
}
}
Test values:
$year='2013';
$period='1';
$typeperiod='s';
$course='8145';
I need to compare their usernames with the values inside Identificador. Only the matchable ( on Moodle) will be enrolled,in bulk, on Moodle course page, without any sort of form.
I've tried to build the idea, but not sure if i'm heading on the right track.
$url=buildURL($year,$period,$typeperiod,$course);
$content_b=doRequest_with_FileGetContents($url);
$results = array();
$allUsers= array();
$allUsers= getallUsers(); //getting all users on Moodle
$dataClip= array();
$dataClip= processXML($content_b); //getting all identificadores from xml file ?
$courseid= required_param('id', PARAM_INT);
$context= get_context_instance(CONTEXT_COURSE, $courseid);//Getting students who are already enrolled
$students= get_role_users(5,$context);
// comparing usernames(identificador values) between Clip and Moodle
if(is_array($dataClip)){ //eliminates warnings of Invalid Argument in foreach
foreach($dataClip as $newdata){
$duplicate=false;
if(is_array($allUsers)){
foreach($allUsers as $dataMoodle){
// if there is a match
if($newdata===$dataMoodle){
// if student not enrolled yet on moodle course page.
if(!$students){
$duplicate=false;
$temp_object= clone $dataMoodle;
$results= $temp_object;
/*
* some code from user_bulk.php
*/
foreach($results as $userid) {
if ($userid == -1) {
continue;
}
if (!isset($SESSION->bulk_users[$userid])) {
$SESSION->bulk_users[$userid] = $userid;
}
}
}
// if there is a match and student already enrolled on moodle course page
$duplicate=true;
continue;
}
//no match. student not on moodle
$duplicate= false;
continue;
}
}
}
}
//after processing redirects back to course page
$urltogo = new moodle_url('/course/view.php', array('id'=>$courseid));
redirect($urltogo);
And this doesn't work. I have developer mode On and no errors/warnings. And no enrolled students neither.
I have users' table users, where I store information like post_count and so on. I want to have ~50 badges and it is going to be even more than that in future.
So, I want to have a page where member of website could go and take the badge, not automatically give him it like in SO. And after he clicks a button called smth like "Take 'Made 10 posts' badge" the system checks if he has posted 10 posts and doesn't have this badge already, and if it's ok, give him the badge and insert into the new table the badge's id and user_id that member couldn't take it twice.
But I have so many badges, so do I really need to put so many if's to check for all badges? What would be your suggestion on this? How can I make it more optimal if it's even possible?
Thank you.
optimal would be IMHO the the following:
have an object for the user with functions that return user specific attributes/metrics that you initialise with the proper user id (you probably wanna make this a singleton/static for some elements...):
<?
class User {
public function initUser($id) {
/* initialise the user. maby load all metrics now, or if they
are intensive on demand when the functions are called.
you can cache them in a class variable*/
}
public function getPostCount() {
// return number of posts
}
public function getRegisterDate() {
// return register date
}
public function getNumberOfLogins() {
// return the number of logins the user has made over time
}
}
?>
have a badge object that is initialised with an id/key and loads dependencies from your database:
<?
class Badge {
protected $dependencies = array();
public function initBadge($id) {
$this->loadDependencies($id);
}
protected function loadDependencies() {
// load data from mysql and store it into dependencies like so:
$dependencies = array(array(
'value' => 300,
'type' => 'PostCount',
'compare => 'greater',
),...);
$this->dependencies = $dependencies;
}
public function getDependencies() {
return $this->dependencies;
}
}
?>
then you could have a class that controls the awarding of batches (you can also do it inside user...)
and checks dependencies and prints failed dependencies etc...
<?
class BadgeAwarder {
protected $badge = null;
protected $user = null;
public function awardBadge($userid,$badge) {
if(is_null($this->badge)) {
$this->badge = new Badge; // or something else for strange freaky badges, passed by $badge
}
$this->badge->initBadge($badge);
if(is_null($this->user)) {
$this->user = new User;
$this->user->initUser($userid);
}
$allowed = $this->checkDependencies();
if($allowed === true) {
// grant badge, print congratulations
} else if(is_array($failed)) {
// sorry, you failed tu full fill thef ollowing dependencies: print_r($failed);
} else {
echo "error?";
}
}
protected function checkDependencies() {
$failed = array();
foreach($this->badge->getDependencies() as $depdency) {
$value = call_user_func(array($this->badge, 'get'.$depdency['type']));
if(!$this->compare($value,$depdency['value'],$dependency['compare'])) {
$failed[] = $dependency;
}
}
if(count($failed) > 0) {
return $failed;
} else {
return true;
}
}
protected function compare($val1,$val2,$operator) {
if($operator == 'greater') {
return ($val1 > $val2);
}
}
}
?>
you can extend to this class if you have very custom batches that require weird calculations.
hope i brought you on the right track.
untested andp robably full of syntax errors.
welcome to the world of object oriented programming. still wanna do this?
Maybe throw the information into a table and check against that? If it's based on the number of posts, have fields for badge_name and post_count and check that way?