Messaging: Method to restrict student messages to teachers only
if(!isteacherinanycourse($USER->id)){ //GVA restriction on messages
if(!isteacherinanycourse($user->id)){
error("Students can only send Moodle messages to teachers.");
}
}
Re: Messaging: Method to restrict student messages to teachers only
Re: Messaging: Method to restrict student messages to teachers only
Thank you Gary.
Re: Messaging: Method to restrict student messages to teachers only
I really, really hope so. Whole site open messaging lacks control and switching it off loses essential teacher/ student intereaction.
Great fix.
Re: Messaging: Method to restrict student messages to teachers only
-- Art
Re: Messaging: Method to restrict student messages to teachers only
When implementing this code change, it doesn't affect the students being able to participate in a chat activity in a particular course, does it? It simply blocks students from being able to send a message to another student by clicking on the Send Message button/link on that student's profile or on the Online Users block, correct?
Thanks,
Ali
Re: Messaging: Method to restrict student messages to teachers only
Chats are different from messages. They must be explicitly turned on and can run on a schedule of your choice, so it does not pose the same issue.
But if you for some reason you wanted a chat where only students could ask questions of teachers, the code above would be a good starting point for identifying who would be eligible to send and receive messages might be a good starting point.
Re: Messaging: Method to restrict student messages to teachers only
I don't like this ptach: it changes Moodle in an old fashioned teacher-centered tool: I prefer the other patch (in the add-on catalog) where you can set times limits and/or courselimits on the use of messaging.
It helps, if you explain to students that it is not a privat MSN, but that you can read all the transcripts in the daily log files. (..and show it their parents?)
Re: Messaging: Method to restrict student messages to teachers only
Hi Gary - this patch has been so good. However since updating to Ver 7.0 the kids have peer-to-peer access again. I am assuming this is to do with the new levels of 'Roles'. Do you have any thoughts please?
Thanks
Greg
Re: Messaging: Method to restrict student messages to teachers only
Is it just me - or am I right in assuming that it has something to do with Ver 1.7 and Roles?? In the meantime - I have had to switch off messaging altogether It wa all getting too graphic for me!!
Greg
Re: Messaging: Method to restrict student messages to teachers only
There seems to be a lack of appreciation that middle school children will use a VLE messaging system for social chit chat when ever they have the opportunity, restricting peer to peer access in school time is essential. Equally as important is the need to allow the kids to share ideas via the VLE out of school. They don't like forums because they are not "cool" and not "private". The result of student to teacher only or total message blocking is the students don't use it socially and as a result don't adopt the VLE "habit" for sharing ideas; they only go there when told to..... Lack of use and inexperience, results in forgotten passwords, user names, inability to navigate etc and this is then used as an excuse for not carrying out VLE tasks. This strengthens the case of those keen to rubbish the whole VLE concept.
So we're sticking with 1.6 for the minute until 1.8 arrives, where hopefully these role and messaging issues will be resolved. From where we are standing 1.7 does seem a bit of a "pot mess" and don't help our VLE cause in a cynical world of teachers disillusioned by ICT initiatives.
Re: Messaging: Method to restrict student messages to teachers only
Re: Messaging: Method to restrict student messages to teachers only

Thanks a lot, Gary.
Jeff
Re: Messaging: Method to restrict student messages to teachers only
Ideas?!
Jeff
Fixed
The code must be added just before
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
Re: Messaging: Method to restrict student messages to teachers only
I think I will have to use this as a half way house. The old message hack does not work which allowed me to block specific students and I need something to prevent abuse.
Thanks for the code.
I hope that messaging will find it's way into roles so I can add it ot my restricted (naughty) role, no blogs, messaging, forums or chat.
Rick.
Re: Messaging: Method to restrict student messages to teachers only
I have tried this on my 1.7.2 test and it worked first time.
So I tried it on my production 1.7.1 I have seen the message to students once but no luck blocking messages since.
I even uploaded the copy of discussion.php from my local server to the production server to see if I made a mistake editing.
Rick.
Re: Messaging: Method to restrict student messages to teachers only
Yes. Thank you very much. Your method is a much simpler implementation. Allowing messaging among students may not be a good idea as it may tempt them to use it for cheating (e.g. sending over answers to quizzes, assignments, etc). The forum is a much better controlled environment for them to exchange opinions and messages.
I have implemented it and is working like a charm.
Re: Messaging: Method to restrict student messages to teachers only
Re: Messaging: Method to restrict student messages to teachers only
I'm thinking that I already have a role that i can use to stop pupils from replying in forums and chats, If I could extend that to messaging using a variation of this hack I would be evan happier.
Rick
Re: Messaging: Method to restrict student messages to teachers only
- Ashlee

Re: Messaging: Method to restrict student messages to teachers only
Re: Messaging: Method to restrict student messages to teachers only
Hi all,
Running 1.8.3 and tried inserting the code in various places including where Jeff suggested (just before the ?>) but to no avail. Anyone any suggestions?
Matt
Re: Messaging: Method to restrict student messages to teachers only
I am attaching the file I use. Unzip and rename to .php. It is from Moodle 1.8.2. Use the complete file at your own risk, but looking at the file will show you where to place the amendment.
Ian
Re: Messaging: Method to restrict student messages to teachers only
Hello Ian,
Many thanks for the reply. I saved that file into my moodle/message folder and renamed it discussion.php, overwriting the standard file. However, I can still log in under a student name and send messages to other students.
Am I forgetting some other setting, or am I saving the file to the wrong place?
Regards,
Matt
Re: Messaging: Method to restrict student messages to teachers only
Ian,
Disregard my last, it has started working! Don't know why, but I just restarted my server, tested and it worked.
Thanks again,
Matt
Re: Messaging: Method to restrict student messages to teachers only
This worked perfect and on the first try with 1.8.3 - many thanks!!
Eric
Re: Messaging: Method to restrict student messages to teachers only
I have installed this hack and it works like a charm so thank you very much Gary! However, I'm using Moodle 1.8.2 and for some reason I have one student who is able to send messages to students and teachers. It's almost like he has a teacher role, but I’ve checked thoroughly and he does not have the role of a teacher anywhere on our Moodle site. I even added him as a global student role and no joy..???
Any ideas?
Sax
Re: Messaging: Method to restrict student messages to teachers only
Re: Messaging: Method to restrict student messages to teachers only
Checked username field and reset password but student is still able to send messages. I would delete the profile but the student has submitted work in lots of different courses.
Thanks for your help regardless,
Sax
Re: Messaging: Method to restrict student messages to teachers only
if ($user->deleted) {
print_header();
print_heading(get_string('userdeleted'));
print_footer();
die;
}
Immediately after this small if block, and before '/// Check if frame&jsless mode selected', add another separate if block as follows:
if (!isteacherinanycourse($USER->id)) {
if (!isteacherinanycourse($user->id)) {
print_header();
print_heading("Students can only send messages to teachers.");
print_footer();
die;
}
}
If a student tries to message another student all they see is the notice. If they try to message a teacher everything works as expected.
Enjoy!
Nick
Re: Messaging: Method to restrict student messages to teachers only
Re: Messaging: Method to restrict student messages to teachers only

Re: Messaging: Method to restrict student messages to teachers only
Do you reckon something similar could be done to automatically show the course teacher on the students contact list?
Basically I'm looking to remove the search altogether because it allows the students to see who's signed up on the site (a big no no).
But I also want them to be able to message the teacher and only the teacher, but the contact list is empty by default.
Cheers
Re: Messaging: Method to restrict student messages to teachers only
Hi Leonardo,
Manish's later post drew me back here. We do something a little different from what you want. We put a "message your instructor" link in the top section of the classroom.
Instructions here http://moodle.org/mod/forum/discuss.php?d=130521#p701500
Re: Messaging: Method to restrict student messages to teachers only
This hack has always worked for us but since upgrading to Moodle 1.9.14 it doesn't seem to work anymore. Is anyone else finding this?
Many thanks
Alex
Moolde 2.0: Method to restrict student messages to teachers only
Has anybody figured out how to do this in Moodle 2.0? I have been analysing the code of message/index.php and following code looks like a ray of hope for the admin user inserted at around line 139:
if ($user2 && !has_capability('moodle/site:doanything', $context)) {
$messageerror = ("Students can only send messages to teachers.");
}
More needs to be done.
Manish.
Re: Moodle 2.0: Method to restrict student messages to teachers only
I have figured it out for admins. In the file message/index.php, the following lines need to be added after the line, "//send message form" (somewhere around line no. 270),
$admins = get_admins();
$isadmin = false;
foreach ($admins as $admin) {
if ($user2->id == $admin->id or $user1->id == $admin->id) {
$isadmin = true;
break;
}
}
if ($isadmin) {
// Continue
} else {
$messageerror = ("Students can send messages to administrator(s) only.");
}
Manish.
Re: Moodle 2.0: Method to restrict student messages to teachers only
Is there a way of doing this in 2.0 for us that are fearful of breaking pages of code? I.e. is there a "clicky" way of doing this?
Thank you
Re: Moodle 2.0: Method to restrict student messages to teachers only
I agree there has to be a better way of doing this, has anyone figured this out I need to allow Student to Teacher and Teacher to Student ONLY and disallow student to student... I thought 2.0 was going to be better but so far everything I have seen it's worse.
Re: Moodle 2.0: Method to restrict student messages to teachers only
I agree there has to be a better way of doing this, has anyone figured this out I need to allow Student to Teacher and Teacher to Student ONLY and disallow student to student... I thought 2.0 was going to be better but so far everything I have seen it's worse.
Well Moodle 2.x is much better than 1.9 based on the quiz and workshop modules alone.
But yes, the messaging module in Moodle 2.x for high school is fail without detailed permissions for student to student messaging. I'd love to be able to have a custom message popup when a student logged in.
Without the ability to remove messaging between students, it's a huge distraction and classroom management problem.
I'll try out Manish's code hack, but I'm always less than thrilled with core hacks because of the major PITA when upgrading.
Re: Messaging: Method to restrict student messages to teachers only
This is an old question. But I think it is still something that needs to be implemented in Moodle core as a possible restriction. At my school, all secondary students have iPads in class all day, and giving them the possibility to send messages to each other during class is just to distracting for them.
There are more elegant ways of coding this, after several hours of looking for a solution, I solved it in two lines of code. It's working for me in Moodle 2.7.
We can add the code to message/index.php, on about line 171, before "if(empty($messageerror))". Here we can add:
if((!user_has_role_assignment($user1->id,3))&&(!user_has_role_assignment($user2->id,3)) $messageerror = "You can only send messages to teachers";
You can test for which roles a user has in the following manner:
if((!user_has_role_assignment($user1->id,3))&&(!user_has_role_assignment($user2->id,3))
$messageerror = "You can only send messages to teachers";
The role id of a teacher is usually 3, and the role id of a student is usually 5, but you can test this looking at the table in Site Administration-> Users -> Permissions -> Define Roles
Please note that one user can have various roles. The function user_has_role_assignment seems to test of which roles he has system wide.
Re: Messaging: Method to restrict student messages to teachers only
Thanks - I have added your hack to the Messaging FAQ documentation in the See also section.
Re: Messaging: Method to restrict student messages to teachers only
Hi Kevin,
Is there any method to restrict the students can see only teachers messages in Chat activity??
Thanks in advance
Re: Messaging: Method to restrict student messages to teachers only
Perhaps a slight modification is in order. I used the code as presented and got the same error Jon Fila reported, where we get the correct URL but a blank page.
Below is Kevin's original code:
if((!user_has_role_assignment($user1->id,3))&&(!user_has_role_assignment($user2->id,3))
$messageerror = "You can only send messages to teachers";
Each (!user_has_role_assignment($user1->,3)) is a separate statement to be tested. Two such statements are combined in the if statement, and the if statement needs its own opening and closing parenthesis. I'm going to space it out to make the idea more clear, but don't copy it the way you see it here
if ( ( !user_has_role_assignment($user1->,3) ) && ( !user_has_role_assignment($user2->id,3) ) )
I made changes to resolve these issues and my correct URL but blank page issue went away.
Below is the code that works in my 2.7.1+ site, and I think that it will resolve the issue Jon Fila had with 2.9.x.
// Thanks to Kevin Bruton c.o. https://moodle.org/mod/forum/discuss.php?d=55934#p1128549 if((!user_has_role_assignment($user1->id,3))&&(!user_has_role_assignment($user2->id,3))) {
$messageerror = "You can only send messages to teachers"; }
I think the lack of closing parenthesis was causing the script on the page to fail after line 171, which is why we get the correct URL but no content. I advise you to try Kevin's original code first. If you get the error described by Jon, try the edited version below. There haven't been enough people posting back over time to know if the error happens on all systems or only some. So again, I offer this as a possible solution if Kevin's fix doesn't quite get you where you were hoping to go.
Re: Messaging: Method to restrict student messages to teachers only
I don't like to double post, but my 30 minutes for editing passed before I could confirm.
Here's a screen shot from my system. My Greg Lind account is a student account. I can look up other students under contacts but I can't message them. I'm a high school teacher like some of the other previous posters, and this restriction will solve a lot of problems I was having!
And below you see the message I received in my teacher account from the student account:
So thanks again to Kevin! If I have a small ability to navigate code, I certainly couldn't have devised his solution! Thanks also to Jon for posting a detailed description of his issue so that we could learn from each other and solve problems.
Re: Messaging: Method to restrict student messages to teachers only
Hi Greg,
I have applied the code and it works perfectly. I made a small modification that I would share. I changed
$messageerror = "You can only send messages to teachers";
for
$messageerror = get_string('errorstudentteacher', 'message');
And in the file message.php in the folder lang > en I added the line
$string ['errorstudentteacher'] = 'You can only send messages to teachers';
But now a new question comes me. Would it be possible to filter the participants so that students only see the teacher?
Cheers.
Re: Messaging: Method to restrict student messages to teachers only
I don't know about filtering participants. That would be great. There's no sense in students seeing lists of people that they can't message.
I'm posting to confirm that this works with with Moodle 2.9.2+ (Build: 20151023).
Re: Messaging: Method to restrict student messages to teachers only
Pasting the code from previous posts works with 3.0+ (Build: 20151126).
However, in the 3.0+ version of the /message/index.php file you (probably) want to paste the code between lines 212 and 213 or thereabouts. In previous versions (e.g. 2.7, 2.9) it was at around line 171.
Re: Messaging: Method to restrict student messages to teachers only
I can confirm it is still working in 3.0.2+ (build 20160129). Here's a screen shot of /message/index.php so you can see how I pasted in the code. Since this seems to be working with everything I've had my hands on, I'll stop posting here until/unless I ever find a version where it doesn't work. In the meantime, it seems like this is a relatively easy line of code that could be included in the core and turned "on" or "off" with a checkbox somewhere in the admin settings.
Reminder: if there's a downside to this hack, it's that you have to apply it each time you update the system. It doesn't take long, it's just something you need to re-apply because an update will get you a new /message/index.php file without the hack. I wrote myself a README.txt type of file with a few things that I want to do every time I update, so now that this hack is in my list of things to do I've been humming along smoothly through four or five updates since I first found this.
Re: Messaging: Method to restrict student messages to teachers only
What is the correct code I should use for moodle 3.0? Please clearly outline all of the steps.
Re: Messaging: Method to restrict student messages to teachers only
Okay I have just tried this and follow the instructions by editing the message/index file...After I logged in as a teacher, I got a message that said "You can only send messages to teachers" but this was on the teacher side. How do I stop that?
Re: Messaging: Method to restrict student messages to teachers only
I'm guessing here, Sarah, but it seems that in your case Moodle is behaving as if you are enrolled as a student. The code we inserted checks to see if a student is trying to send a message to another student. If this situation is true the student sees the message. If it's false nothing happens. Check to see how you are enrolled in the class. Maybe you're enrolled as a teacher and a student. (?) Again, it's a guess, but I can't think of anything else that would/could cause what you're experiencing.
Well, I suppose it could be related to this: your first post appears immediately under a screenshot of the code in question posted in the latest 3.0.2+ build. Your first post suggests that you did not see the most recent post about this technique, which addresses your question. If you didn't read the later posts you may have inserted the code into the wrong area of your 3.0.x installation and I suppose that could cause this problem, though I don't think this second scenario is likely.
Re: Messaging: Method to restrict student messages to teachers only
Updated to 3.0.4 and it's still working. I'm posting here because in my previous post with a screenshot I have the code into the wrong spot! I discovered the error today when I updated my system to 3.0.4:
Re: Messaging: Method to restrict student messages to teachers only
Confirm that works for ver 3.1+
Re: Messaging: Method to restrict student messages to teachers only
I was not able to get this to work in 3.1. If the students are on the main messaging page, they get the "You can only send messages to teachers" verbiage, but if they go directly to a student's profile, they can click the "message" button and send that student a message normally. Is there maybe another place in the 3.1 code that needs to be edited? Thanks!
Re: Messaging: Method to restrict student messages to teachers only
Try/check these things:
- did you insert the code in the correct spot? I had it in the wrong position in an earlier post where I left a screenshot, not realizing I was showing people how to do it incorrectly! My most recent screenshot is correct on my 3.0.4 system. I have seen the behavior your describe but it is resolved now.
I'm pretty sure the problem was that I had the code in the wrong spot. I just checked to make sure that I no longer have this issue.I realize your system is updated and I haven't updated mine yet. Today is my last group of final exams and I'm not going to try to update until everything is done and all grades are posted in our SIS.
- I'm pretty sure you can change permissions so that students can't view/browse a list of other class participants. [EDIT] Yeah, here's a screen grab of my current settings (3.0.4)

Re: Messaging: Method to restrict student messages to teachers only
Here's the pertinent info:
Role permissions for a course can be changed in Course administration > Users > Permissions and for a particular activity in Activity administration > Permissions.
Click the Allow icon (+) opposite a capability to give permission to additional roles or the Prevent icon (X) to take away permission.
Re: Messaging: Method to restrict student messages to teachers only
If you're looking for a quick fix it seems this is no longer the ticket. I imagine the same functionality can be provided some other way, but I don't have time right now and perhaps I lack the skill. If I find a solution I'll post back.
Re: Messaging: Method to restrict student messages to teachers only
I think I put the code in the right place:
Re: Messaging: Method to restrict student messages to teachers only
I want to allow students to see the participant list and profiles, though. So I don't think changing permissions will help. I could also use permissions to change who can send messages, but that would prevent students from messaging teachers, too. I think the code change it my only option, but I'm not familiar enough to figure out how to use the code to restrict the message button on profiles
Re: Messaging: Method to restrict student messages to teachers only
Re: Messaging: Method to restrict student messages to teachers only
Allow me to change my answer. It does work in 3.1.2 sort of. It does not prevent though the send message pop up from sending a message. How do I disable this pop up then?
Re: Messaging: Method to restrict student messages to teachers only
I am referring to this dialogue pop up. This sends the message to any student regardless of the restrictions set by your code.

Re: Messaging: Method to restrict student messages to teachers only
I would love to see this as a separate block, so that teachers who are not site admins would be able to add this functionality to their courses without site admins needing to edit base code.
Currently, our site admins have messaging disabled site-wide, so I can't test this, but I was wondering if something like this:
https://moodle.org/plugins/block_messageteacher
would work as a workaround. I wonder if you had that block visible, but the messaging block hidden, if then the only interaction would be between teacher and student. I understand that it wouldn't block messaging from a person's profile.
Re: Messaging: Method to restrict student messages to teachers only
ok let check and make a code for this i will let you know in couple of minutes.
Re: Messaging: Method to restrict student messages to teachers only
Hi
+1 the code works really well. I was wondering if anyone knew where to start looking to do the same for the moodle mobile app? We have just been testing on 3.1.3+ and it ignores additional code and allows student to student messaging - i can only assume the mobile app code is different due to the different device connecting but I dont know where to start looking?
I've googled as best i can but i can find is either this forum post or the FAQ about enabling/disabling messaging for specific students rather than based on recipient.
Thanks in advance
Ian
Re: Messaging: Method to restrict student messages to teachers only
I've raised this as a seperate thread within the moodle for mobile forum https://moodle.org/mod/forum/discuss.php?d=345772.