Skip to content

Commit

Permalink
Changes to support guest user across any course (guest/guest)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed Jun 9, 2002
1 parent e2fc256 commit da5c172
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 56 deletions.
15 changes: 15 additions & 0 deletions admin/user.php
Expand Up @@ -43,6 +43,21 @@
$USER->loggedin = true; $USER->loggedin = true;
$USER->admin = true; $USER->admin = true;
$USER->teacher["$course->id"] = true; $USER->teacher["$course->id"] = true;

// This is also a good time to make the Guest User

$user = NULL;
$user->firstname = "Guest";
$user->lastname = "User";
$user->username = "guest"; // This user is identified by this
$user->password = md5("guest");
$user->email = "root@localhost";
$user->confirmed = 1;
$user->timemodified = time();

if (! $guest->user = insert_record("user", $user)) {
notify("Could not create guest user record !!!");
}
} }


require_login(); require_login();
Expand Down
16 changes: 12 additions & 4 deletions course/edit.html
Expand Up @@ -13,15 +13,23 @@
</td> </td>
</tr> </tr>
<tr valign=top> <tr valign=top>
<td><P>Password:</td> <td><P>Summary:</td>
<td><TEXTAREA NAME=summary COLS=50 ROWS=10 WRAP=virtual><? p($form->summary) ?></TEXTAREA>
<? formerr($err["summary"]) ?>
</td>
</tr>
<tr valign=top>
<td><P>Entry Key:</td>
<td><input type="text" name="password" size=25 value="<? p($form->password) ?>"> <td><input type="text" name="password" size=25 value="<? p($form->password) ?>">
<? formerr($err["password"]) ?> <? formerr($err["password"]) ?>
</td> </td>
</tr> </tr>
<tr valign=top> <tr valign=top>
<td><P>Summary:</td> <td><P>Open to guests?:</td>
<td><TEXTAREA NAME=summary COLS=50 ROWS=10 WRAP=virtual><? p($form->summary) ?></TEXTAREA> <td><?
<? formerr($err["summary"]) ?> $choices["0"] = "No, only students are allowed";
$choices["1"] = "Yes, allow \"guest\" student in";
choose_from_menu ($choices, "guest", $form->guest, "") ?>
</td> </td>
</tr> </tr>
<tr valign=top> <tr valign=top>
Expand Down
22 changes: 15 additions & 7 deletions course/enrol.php
Expand Up @@ -16,10 +16,14 @@


if ($password == $course->password) { if ($password == $course->password) {


if (! enrol_student_in_course($USER->id, $course->id)) { if (isguest()) {
error("An error occurred while trying to enrol you."); add_to_log($course->id, "course", "guest", "view.php?id=$course->id", "$USER->id");
} else {
if (! enrol_student_in_course($USER->id, $course->id)) {
error("An error occurred while trying to enrol you.");
}
add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");
} }
add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");


$USER->student["$id"] = true; $USER->student["$id"] = true;


Expand All @@ -44,12 +48,16 @@
} }


if ($course->password == "") { // no password, so enrol if ($course->password == "") { // no password, so enrol
if (! enrol_student_in_course($USER->id, $course->id)) {
error("An error occurred while trying to enrol you."); if (isguest()) {
add_to_log($course->id, "course", "guest", "view.php?id=$course->id", "$USER->id");
} else {
if (! enrol_student_in_course($USER->id, $course->id)) {
error("An error occurred while trying to enrol you.");
}
add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");
} }


add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");

$USER->student["$id"] = true; $USER->student["$id"] = true;


if ($SESSION->wantsurl) { if ($SESSION->wantsurl) {
Expand Down
2 changes: 1 addition & 1 deletion course/index.php
Expand Up @@ -9,7 +9,7 @@


if ($courses = get_records("course", "category", $cat, "fullname ASC")) { if ($courses = get_records("course", "category", $cat, "fullname ASC")) {


foreach ($courses as $key => $course) { foreach ($courses as $course) {
print_course($course); print_course($course);
echo "<BR>\n"; echo "<BR>\n";
} }
Expand Down
25 changes: 18 additions & 7 deletions course/lib.php
Expand Up @@ -124,7 +124,8 @@ function print_course($course) {
print_simple_box_start("CENTER", "80%"); print_simple_box_start("CENTER", "80%");


echo "<TABLE WIDTH=100%>"; echo "<TABLE WIDTH=100%>";
echo "<TR VALIGN=top><TD VALIGN=top WIDTH=50%>"; echo "<TR VALIGN=top>";
echo "<TD VALIGN=top WIDTH=50%>";
echo "<P><FONT SIZE=3><B><A HREF=\"view.php?id=$course->id\">$course->fullname</A></B></FONT></P>"; echo "<P><FONT SIZE=3><B><A HREF=\"view.php?id=$course->id\">$course->fullname</A></B></FONT></P>";
if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
WHERE u.id = t.user AND t.course = '$course->id' WHERE u.id = t.user AND t.course = '$course->id'
Expand All @@ -135,13 +136,23 @@ function print_course($course) {
echo "$course->teacher: <A HREF=\"../user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>"; echo "$course->teacher: <A HREF=\"../user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname</A><BR>";
} }
echo "</FONT></P>"; echo "</FONT></P>";
} }
echo "</TD><TD VALIGN=top WIDTH=50%>"; if ($course->guest or ($course->password == "")) {
echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>"; echo "<A TITLE=\"Guest user allowed\" HREF=\"view.php?id=$course->id\">";
echo "</TD></TR>"; echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"../user/user.gif\"></A>&nbsp;&nbsp;";
echo "</TABLE>"; }
if ($course->password) {
echo "<A TITLE=\"Requires a Course entry key\" HREF=\"view.php?id=$course->id\">";
echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"../pix/i/key.gif\"></A>";
}


echo "</TD><TD VALIGN=top WIDTH=50%>";
echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
echo "</TD></TR>";
echo "</TABLE>";


print_simple_box_end(); print_simple_box_end();
} }


function print_headline($text, $size=2) { function print_headline($text, $size=2) {
Expand Down
39 changes: 20 additions & 19 deletions lib/db/mysql.sql
Expand Up @@ -14,20 +14,21 @@
# Table structure for table `course` # Table structure for table `course`
# #


CREATE TABLE course ( CREATE TABLE `course` (
id int(10) unsigned NOT NULL auto_increment, `id` int(10) unsigned NOT NULL auto_increment,
category int(10) unsigned NOT NULL default '0', `category` int(10) unsigned NOT NULL default '0',
password varchar(50) NOT NULL default '', `password` varchar(50) NOT NULL default '',
fullname varchar(254) NOT NULL default '', `fullname` varchar(254) NOT NULL default '',
shortname varchar(15) NOT NULL default '', `shortname` varchar(15) NOT NULL default '',
summary text NOT NULL, `summary` text NOT NULL,
format tinyint(4) NOT NULL default '1', `format` tinyint(4) NOT NULL default '1',
teacher varchar(100) NOT NULL default 'Teacher', `teacher` varchar(100) NOT NULL default 'Teacher',
student varchar(100) NOT NULL default 'Student', `student` varchar(100) NOT NULL default 'Student',
startdate int(10) unsigned NOT NULL default '0', `guest` tinyint(1) unsigned NOT NULL default '0',
enddate int(10) unsigned NOT NULL default '0', `startdate` int(10) unsigned NOT NULL default '0',
timemodified int(10) unsigned NOT NULL default '0', `enddate` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (id) `timemodified` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM; ) TYPE=MyISAM;
# -------------------------------------------------------- # --------------------------------------------------------


Expand Down Expand Up @@ -165,11 +166,11 @@ CREATE TABLE user (
# Table structure for table `user_admins` # Table structure for table `user_admins`
# #


CREATE TABLE user_admins ( CREATE TABLE `user_admins` (
id int(10) unsigned NOT NULL auto_increment, `id` int(10) unsigned NOT NULL auto_increment,
user int(10) unsigned NOT NULL default '0', `user` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (id), PRIMARY KEY (`id`),
UNIQUE KEY id (id) UNIQUE KEY `id` (`id`)
) TYPE=MyISAM COMMENT='One record per administrator user'; ) TYPE=MyISAM COMMENT='One record per administrator user';
# -------------------------------------------------------- # --------------------------------------------------------


Expand Down
51 changes: 35 additions & 16 deletions lib/moodlelib.php
Expand Up @@ -705,13 +705,14 @@ function update_user_in_db() {
} }
} }


function require_login($course=0) { function require_login($courseid=0) {
// if they aren't already logged in, then send them off to login // This function checks that the current user is logged in, and optionally
// $course is optional - if left out then it just requires that // whether they are "logged in" or allowed to be in a particular course.
// that they have an account on the system. // If not, then it redirects them to the site login or course enrolment.


global $CFG, $SESSION, $USER, $FULLME, $HTTP_REFERER, $PHPSESSID; global $CFG, $SESSION, $USER, $FULLME, $HTTP_REFERER, $PHPSESSID;


// First check that the user is logged in to the site.
if (! (isset( $USER->loggedin ) && $USER->confirmed) ) { if (! (isset( $USER->loggedin ) && $USER->confirmed) ) {
$SESSION->wantsurl = $FULLME; $SESSION->wantsurl = $FULLME;
$SESSION->fromurl = $HTTP_REFERER; $SESSION->fromurl = $HTTP_REFERER;
Expand All @@ -721,20 +722,28 @@ function require_login($course=0) {
redirect("$CFG->wwwroot/login/"); redirect("$CFG->wwwroot/login/");
} }
die; die;

} else if ($course) {
if (! ($USER->student[$course] || $USER->teacher[$course] || $USER->admin ) ) {
if (!record_exists("course", "id", $course)) {
error("That course doesn't exist");
}

$SESSION->wantsurl = $FULLME;
redirect("$CFG->wwwroot/course/enrol.php?id=$course");
die;
}
} }

// Next, check if the user can be in a particular course
if ($courseid) {
if ($USER->student[$courseid] || $USER->teacher[$courseid] || $USER->admin) {
update_user_in_db();
return; // user is a member of this course.
}
if (! $course = get_record("course", "id", $courseid)) {
error("That course doesn't exist");
}
if ($course->guest && ($USER->username == "guest")) {
update_user_in_db();
return; // user is a guest and this course allows guests
}


update_user_in_db(); // Not allowed in the course, so see if they want to enrol

$SESSION->wantsurl = $FULLME;
redirect("$CFG->wwwroot/course/enrol.php?id=$courseid");
die;
}
} }




Expand Down Expand Up @@ -794,6 +803,16 @@ function isstudent($course, $userid=0) {
return record_exists_sql("SELECT * FROM user_students WHERE user='$userid' AND course='$course'"); return record_exists_sql("SELECT * FROM user_students WHERE user='$userid' AND course='$course'");
} }


function isguest($userid=0) {
global $USER;

if (!$userid) {
return ($USER->username == "guest");
}

return record_exists_sql("SELECT * FROM user WHERE user='$userid' AND username = 'guest' ");
}



function reset_login_count() { function reset_login_count() {
global $SESSION; global $SESSION;
Expand Down
4 changes: 4 additions & 0 deletions mod/journal/edit.php
Expand Up @@ -14,6 +14,10 @@


require_login($course->id); require_login($course->id);


if (isguest()) {
error("Guests are not allowed to edit journals", $HTTP_REFERER);
}

if (! $journal = get_record("journal", "id", $cm->instance)) { if (! $journal = get_record("journal", "id", $cm->instance)) {
error("Course module is incorrect"); error("Course module is incorrect");
} }
Expand Down
2 changes: 0 additions & 2 deletions mod/journal/view.php
Expand Up @@ -15,7 +15,6 @@


require_login($course->id); require_login($course->id);



if (! $journal = get_record("journal", "id", $cm->instance)) { if (! $journal = get_record("journal", "id", $cm->instance)) {
error("Course module is incorrect"); error("Course module is incorrect");
} }
Expand Down Expand Up @@ -51,7 +50,6 @@


if ($timenow > $timestart) { if ($timenow > $timestart) {



print_simple_box_start("center"); print_simple_box_start("center");


if ($timenow < $timefinish) { if ($timenow < $timefinish) {
Expand Down
4 changes: 4 additions & 0 deletions mod/survey/save.php
Expand Up @@ -10,6 +10,10 @@
error("You are not supposed to use this script like that."); error("You are not supposed to use this script like that.");
} }


if (isguest()) {
error("Guests are not allowed to answer surveys", $HTTP_REFERER);
}

require_variable($id); // Course Module ID require_variable($id); // Course Module ID


if (! $cm = get_record("course_modules", "id", $id)) { if (! $cm = get_record("course_modules", "id", $id)) {
Expand Down
Binary file added pix/i/key.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit da5c172

Please sign in to comment.