]> git.mjollnir.org Git - moodle.git/commitdiff
Changes to support guest user across any course (guest/guest)
authormartin <martin>
Sun, 9 Jun 2002 14:14:07 +0000 (14:14 +0000)
committermartin <martin>
Sun, 9 Jun 2002 14:14:07 +0000 (14:14 +0000)
admin/user.php
course/edit.html
course/enrol.php
course/index.php
course/lib.php
lib/db/mysql.sql
lib/moodlelib.php
mod/journal/edit.php
mod/journal/view.php
mod/survey/save.php
pix/i/key.gif [new file with mode: 0755]

index 41321732f16d903d18c1e97cfd11aedd47046320..de77af40337d8f04c9e368cd2e1ac6421c3ecc8d 100644 (file)
         $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();
index d528f3b0a68016cb2ba64c5faeadce88e4e809b6..f0991ed270d77e3de98d1998ff06a02d947cf70c 100644 (file)
        </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>
index f816cc1d230f7da6a1d8547f3251ebf1e1469e97..349338eed9b235e68040e1e1fbc5372fa4d22fd9 100644 (file)
 
         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;
             
     }
 
     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) {
index 21a667956fc85b492679821405dd1ff5557e5041..e45944ae02b704ffcadef0b32233fda8dce2684f 100644 (file)
@@ -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";
         }
index 109f76a542cb98336d12349fea02887f2d1cf28c..049479581b40beb2e7516dc1c1ec2ecf5c194a31 100644 (file)
@@ -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) {
index ead6483398576b7d1a298d3afe8b7b8cf06aae30..efb3aa403ae72e9f0c2d1ec6a99665cb51915a0f 100644 (file)
 # 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';
 # --------------------------------------------------------
 
index 52a8673b1ec5f64ed0ceab4040e1571aa1f3c996..6cd2e0d457cc362c7b15159defdac82ff611cb04 100644 (file)
@@ -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;
index fdd575b09d85b6657c4f4def9882bf5fcb46c8f7..d129fd6b162cc3e0e442db0777556b4a77bdb9c8 100644 (file)
 
     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");
     }
index b5f940677643629e6edb635bb60dfcffdfd9ea75..62fe7197d33e50139ce7da7c3137bd3c05332fab 100644 (file)
@@ -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) {
index 665389b21f41a9af3b3fc8c75dfc955715429406..ab59327a01c594c0c7106fbbbbfe1e1b9e4d505d 100644 (file)
         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 (executable)
index 0000000..b45a1db
Binary files /dev/null and b/pix/i/key.gif differ