From da5c172a07914eed331cfd23125f457526dbef3d Mon Sep 17 00:00:00 2001 From: martin Date: Sun, 9 Jun 2002 14:14:07 +0000 Subject: [PATCH] Changes to support guest user across any course (guest/guest) --- admin/user.php | 15 +++++++++++++ course/edit.html | 16 ++++++++++---- course/enrol.php | 22 +++++++++++++------ course/index.php | 2 +- course/lib.php | 25 +++++++++++++++------ lib/db/mysql.sql | 39 +++++++++++++++++---------------- lib/moodlelib.php | 51 +++++++++++++++++++++++++++++-------------- mod/journal/edit.php | 4 ++++ mod/journal/view.php | 2 -- mod/survey/save.php | 4 ++++ pix/i/key.gif | Bin 0 -> 929 bytes 11 files changed, 124 insertions(+), 56 deletions(-) create mode 100755 pix/i/key.gif diff --git a/admin/user.php b/admin/user.php index 41321732f1..de77af4033 100644 --- a/admin/user.php +++ b/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(); diff --git a/course/edit.html b/course/edit.html index d528f3b0a6..f0991ed270 100644 --- a/course/edit.html +++ b/course/edit.html @@ -13,15 +13,23 @@ -

Password: +

Summary: + + + + + +

Entry Key: -

Summary: - - +

Open to guests?: + guest, "") ?> diff --git a/course/enrol.php b/course/enrol.php index f816cc1d23..349338eed9 100644 --- a/course/enrol.php +++ b/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) { diff --git a/course/index.php b/course/index.php index 21a667956f..e45944ae02 100644 --- a/course/index.php +++ b/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 "
\n"; } diff --git a/course/lib.php b/course/lib.php index 109f76a542..049479581b 100644 --- a/course/lib.php +++ b/course/lib.php @@ -124,7 +124,8 @@ function print_course($course) { print_simple_box_start("CENTER", "80%"); echo ""; - echo ""; + echo ""; - echo "
"; + echo "
"; echo "

id\">$course->fullname

"; 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: id&course=$site->id\">$teacher->firstname $teacher->lastname
"; } echo "

"; - } - echo "
"; - echo "

".text_to_html($course->summary)."

"; - echo "
"; + } + if ($course->guest or ($course->password == "")) { + echo "id\">"; + echo "\"\"  "; + } + if ($course->password) { + echo "id\">"; + echo "\"\""; + } + + + echo ""; + echo "

".text_to_html($course->summary)."

"; + echo ""; + echo ""; - print_simple_box_end(); + print_simple_box_end(); } function print_headline($text, $size=2) { diff --git a/lib/db/mysql.sql b/lib/db/mysql.sql index ead6483398..efb3aa403a 100644 --- a/lib/db/mysql.sql +++ b/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'; # -------------------------------------------------------- diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 52a8673b1e..6cd2e0d457 100644 --- a/lib/moodlelib.php +++ b/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; diff --git a/mod/journal/edit.php b/mod/journal/edit.php index fdd575b09d..d129fd6b16 100644 --- a/mod/journal/edit.php +++ b/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"); } diff --git a/mod/journal/view.php b/mod/journal/view.php index b5f9406776..62fe7197d3 100644 --- a/mod/journal/view.php +++ b/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) { diff --git a/mod/survey/save.php b/mod/survey/save.php index 665389b21f..ab59327a01 100644 --- a/mod/survey/save.php +++ b/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)) { diff --git a/pix/i/key.gif b/pix/i/key.gif new file mode 100755 index 0000000000000000000000000000000000000000..b45a1db3ab6de9c73fa79c462377cf87675eb326 GIT binary patch literal 929 zcmeH`%S%)N6o=1f*RG1qbw*1~#mW@5qJl_~M9W7$!W2Y}Oe+uyDJxwF8i^FP2~0w8 zArZAG2~^YwD;$F`ha$@~$VHguW5%%=@B7ZZzZ~g5XxmvF&g%DZ&d15i&rMA`&k;uw z`3gWdnIf5h7-ADx4!oR1;aCAgKmrs%19TV;#G2ytB^V;qHE04tDVFgIjx=ZalO>o* zajs&)fi;ij)uE_fNme})com`sr(57<&8qe*Pz*RF#i1i8EZhbB-UM#Y zynq4B``|6 zqZfWP+>Jk7YRM^^8hm=O{nMk&+b))CPY4Q2t}*5#_q5wvlgt4J-jCIuib~H(E*dx( bcp@viSzPMi>n%NbZ4=L`?8EzQw