Skip to content
Browse files

Changes to support guest user across any course (guest/guest)

  • Loading branch information...
1 parent e2fc256 commit da5c172a07914eed331cfd23125f457526dbef3d martin committed Jun 9, 2002
Showing with 124 additions and 56 deletions.
  1. +15 −0 admin/user.php
  2. +12 −4 course/edit.html
  3. +15 −7 course/enrol.php
  4. +1 −1 course/index.php
  5. +18 −7 course/lib.php
  6. +20 −19 lib/db/mysql.sql
  7. +35 −16 lib/moodlelib.php
  8. +4 −0 mod/journal/edit.php
  9. +0 −2 mod/journal/view.php
  10. +4 −0 mod/survey/save.php
  11. BIN pix/i/key.gif
View
15 admin/user.php
@@ -43,6 +43,21 @@
$USER->loggedin = true;
$USER->admin = 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();
View
16 course/edit.html
@@ -13,15 +13,23 @@
</td>
</tr>
<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) ?>">
<? formerr($err["password"]) ?>
</td>
</tr>
<tr valign=top>
- <td><P>Summary:</td>
- <td><TEXTAREA NAME=summary COLS=50 ROWS=10 WRAP=virtual><? p($form->summary) ?></TEXTAREA>
- <? formerr($err["summary"]) ?>
+ <td><P>Open to guests?:</td>
+ <td><?
+ $choices["0"] = "No, only students are allowed";
+ $choices["1"] = "Yes, allow \"guest\" student in";
+ choose_from_menu ($choices, "guest", $form->guest, "") ?>
</td>
</tr>
<tr valign=top>
View
22 course/enrol.php
@@ -16,10 +16,14 @@
if ($password == $course->password) {
- 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;
@@ -44,12 +48,16 @@
}
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;
if ($SESSION->wantsurl) {
View
2 course/index.php
@@ -9,7 +9,7 @@
if ($courses = get_records("course", "category", $cat, "fullname ASC")) {
- foreach ($courses as $key => $course) {
+ foreach ($courses as $course) {
print_course($course);
echo "<BR>\n";
}
View
25 course/lib.php
@@ -124,7 +124,8 @@ function print_course($course) {
print_simple_box_start("CENTER", "80%");
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>";
if ($teachers = get_records_sql("SELECT u.* FROM user u, user_teachers t
WHERE u.id = t.user AND t.course = '$course->id'
@@ -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 "</FONT></P>";
- }
- echo "</TD><TD VALIGN=top WIDTH=50%>";
- echo "<P><FONT SIZE=2>".text_to_html($course->summary)."</FONT></P>";
- echo "</TD></TR>";
- echo "</TABLE>";
+ }
+ if ($course->guest or ($course->password == "")) {
+ echo "<A TITLE=\"Guest user allowed\" HREF=\"view.php?id=$course->id\">";
+ echo "<IMG VSPACE=4 ALT=\"\" HEIGHT=16 WIDTH=16 BORDER=0 SRC=\"../user/user.gif\"></A>&nbsp;&nbsp;";
+ }
+ 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) {
View
39 lib/db/mysql.sql
@@ -14,20 +14,21 @@
# Table structure for table `course`
#
-CREATE TABLE course (
- id int(10) unsigned NOT NULL auto_increment,
- category int(10) unsigned NOT NULL default '0',
- password varchar(50) NOT NULL default '',
- fullname varchar(254) NOT NULL default '',
- shortname varchar(15) NOT NULL default '',
- summary text NOT NULL,
- format tinyint(4) NOT NULL default '1',
- teacher varchar(100) NOT NULL default 'Teacher',
- student varchar(100) NOT NULL default 'Student',
- startdate int(10) unsigned NOT NULL default '0',
- enddate int(10) unsigned NOT NULL default '0',
- timemodified int(10) unsigned NOT NULL default '0',
- PRIMARY KEY (id)
+CREATE TABLE `course` (
+ `id` int(10) unsigned NOT NULL auto_increment,
+ `category` int(10) unsigned NOT NULL default '0',
+ `password` varchar(50) NOT NULL default '',
+ `fullname` varchar(254) NOT NULL default '',
+ `shortname` varchar(15) NOT NULL default '',
+ `summary` text NOT NULL,
+ `format` tinyint(4) NOT NULL default '1',
+ `teacher` varchar(100) NOT NULL default 'Teacher',
+ `student` varchar(100) NOT NULL default 'Student',
+ `guest` tinyint(1) unsigned NOT NULL default '0',
+ `startdate` int(10) unsigned NOT NULL default '0',
+ `enddate` int(10) unsigned NOT NULL default '0',
+ `timemodified` int(10) unsigned NOT NULL default '0',
+ PRIMARY KEY (`id`)
) TYPE=MyISAM;
# --------------------------------------------------------
@@ -165,11 +166,11 @@ CREATE TABLE user (
# Table structure for table `user_admins`
#
-CREATE TABLE user_admins (
- id int(10) unsigned NOT NULL auto_increment,
- user int(10) unsigned NOT NULL default '0',
- PRIMARY KEY (id),
- UNIQUE KEY id (id)
+CREATE TABLE `user_admins` (
+ `id` int(10) unsigned NOT NULL auto_increment,
+ `user` int(10) unsigned NOT NULL default '0',
+ PRIMARY KEY (`id`),
+ UNIQUE KEY `id` (`id`)
) TYPE=MyISAM COMMENT='One record per administrator user';
# --------------------------------------------------------
View
51 lib/moodlelib.php
@@ -705,13 +705,14 @@ function update_user_in_db() {
}
}
-function require_login($course=0) {
-// if they aren't already logged in, then send them off to login
-// $course is optional - if left out then it just requires that
-// that they have an account on the system.
+function require_login($courseid=0) {
+// This function checks that the current user is logged in, and optionally
+// whether they are "logged in" or allowed to be in a particular course.
+// If not, then it redirects them to the site login or course enrolment.
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) ) {
$SESSION->wantsurl = $FULLME;
$SESSION->fromurl = $HTTP_REFERER;
@@ -721,20 +722,28 @@ function require_login($course=0) {
redirect("$CFG->wwwroot/login/");
}
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;
+ }
}
@@ -794,6 +803,16 @@ function isstudent($course, $userid=0) {
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() {
global $SESSION;
View
4 mod/journal/edit.php
@@ -14,6 +14,10 @@
require_login($course->id);
+ if (isguest()) {
+ error("Guests are not allowed to edit journals", $HTTP_REFERER);
+ }
+
if (! $journal = get_record("journal", "id", $cm->instance)) {
error("Course module is incorrect");
}
View
2 mod/journal/view.php
@@ -15,7 +15,6 @@
require_login($course->id);
-
if (! $journal = get_record("journal", "id", $cm->instance)) {
error("Course module is incorrect");
}
@@ -51,7 +50,6 @@
if ($timenow > $timestart) {
-
print_simple_box_start("center");
if ($timenow < $timefinish) {
View
4 mod/survey/save.php
@@ -10,6 +10,10 @@
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
if (! $cm = get_record("course_modules", "id", $id)) {
View
BIN 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.
Something went wrong with that request. Please try again.