From 86aa7ccfdf100bc028f7d22a80dfb3a724f04b6b Mon Sep 17 00:00:00 2001
From: moodler <moodler>
Date: Sun, 20 Jul 2003 13:53:31 +0000
Subject: [PATCH] New feature - "Web link" resources can now specify a popup
 window

fullscreen is currently the only feature left out of this .. I had
trouble getting it to work consistently so I've left it out as being
more troubles than it's worth.
---
 course/lib.php           |  21 ++++++-
 lib/javascript.php       |  41 ++++++++++--
 lib/weblib.php           |  29 ++++++---
 mod/resource/details.php | 133 +++++++++++++++++++++++++++++++++++++--
 mod/resource/lib.php     |  26 ++++++++
 mod/resource/version.php |   2 +-
 6 files changed, 230 insertions(+), 22 deletions(-)

diff --git a/course/lib.php b/course/lib.php
index f8c02c938b..02e39144c7 100644
--- a/course/lib.php
+++ b/course/lib.php
@@ -438,6 +438,7 @@ function get_array_of_activities($courseid) {
 //  section - the number of the section (eg week or topic)
 //  name - the name of the instance
 //  visible - is the instance visible or not
+//  extra - contains extra string to include in any link
 
     $mod = array();
 
@@ -458,6 +459,19 @@ function get_array_of_activities($courseid) {
                    $mod[$seq]->section = $section->section;
                    $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
                    $mod[$seq]->visible = $rawmods[$seq]->visible;
+                   $mod[$seq]->extra = "";
+                   
+                   // This part is an ugly hack that doesn't belong here//
+                   if ($mod[$seq]->mod == "resource") {
+                       if ($resource = get_record("resource", "id", $rawmods[$seq]->instance)) {
+                           if ($resource->type == 5 and $resource->alltext) {
+                               $mod[$seq]->extra = urlencode("onClick=\"return ".
+                                                   "openpopup('/mod/resource/view.php?id=".
+                                                   $mod[$seq]->cm.
+                                                   "','resource','$resource->alltext');\"");
+                           }
+                       }
+                   }
                }
             }
         }
@@ -650,10 +664,15 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false,
                          " href=\"mod.php?moveto=$mod->id\">$strmovehere</a></font><br />\n";
                 }
                 $instancename = urldecode($modinfo[$modnumber]->name);
+                if (!empty($modinfo[$modnumber]->extra)) {
+                    $extra = urldecode($modinfo[$modnumber]->extra);
+                } else {
+                    $extra = "";
+                }
                 $link_css = $mod->visible ? "" : " class=\"dimmed\" ";
                 echo "<img src=\"$CFG->wwwroot/mod/$mod->modname/icon.gif\"".
                      " height=16 width=16 alt=\"$mod->modfullname\">".
-                     " <font size=2><a title=\"$mod->modfullname\" $link_css ".
+                     " <font size=2><a title=\"$mod->modfullname\" $link_css $extra".
                      " href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename</a></font>";
             }
             if (isediting($course->id)) {
diff --git a/lib/javascript.php b/lib/javascript.php
index 1577dfc792..74a51cb7d0 100644
--- a/lib/javascript.php
+++ b/lib/javascript.php
@@ -15,11 +15,15 @@ function fillmessagebox(text) {
   document.form.message.value = text;
 }
 
-function openpopup(url,name,height,width) {
+function openpopup(url,name,options,fullscreen) {
   fullurl = "<?php echo $CFG->wwwroot ?>" + url;
-  options = "menubar=0,location=0,scrollbars,resizable,width="+width+",height="+height;
-  windowobj = window.open(fullurl,name, options);
+  windowobj = window.open(fullurl,name,options);
+  if (fullscreen) {
+     windowobj.moveTo(0,0);
+     windowobj.resizeTo(screen.availWidth,screen.availHeight); 
+  }
   windowobj.focus();
+  return false;
 }
 
 function copyrichtext(textname) { 
@@ -43,15 +47,40 @@ function inserttext(text) {
     }
     echo "  text = ' ' + text + ' ';\n";
     echo "  if ( $insertfield.createTextRange && $insertfield.caretPos) {\n";
-    echo "      var caretPos = $insertfield.caretPos;\n";
-    echo "      caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;\n";
+    echo "    var caretPos = $insertfield.caretPos;\n";
+    echo "    caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;\n";
     echo "  } else {\n";
-    echo "      $insertfield.value  += text;\n";
+    echo "    $insertfield.value  += text;\n";
     echo "  }\n";
     echo "  $insertfield.focus();\n";
 ?>
 }
 
+function lockoptions(form, master, subitems) {
+  // subitems is an array of names of sub items
+  // requires that each item in subitems has a 
+  // companion hidden item in the form with the 
+  // same name but prefixed by "h"
+  if (eval("document."+form+"."+master+".checked")) {
+    for (i=0; i<subitems.length; i++) {
+      unlockoption(form, subitems[i]);
+    }
+  } else {
+    for (i=0; i<subitems.length; i++) {
+      lockoption(form, subitems[i]);
+    }
+  }
+  return(true);
+}
+function lockoption(form,item) {
+  eval("document."+form+"."+item+".disabled=true");/* IE thing */
+  eval("document."+form+".h"+item+".value=1");
+}
+function unlockoption(form,item) {
+  eval("document."+form+"."+item+".disabled=false");/* IE thing */
+  eval("document."+form+".h"+item+".value=0");
+}
+
 <?php if ($focus) { echo "function setfocus() { document.$focus.focus() }\n"; } ?>
 
 // done hiding -->
diff --git a/lib/weblib.php b/lib/weblib.php
index 2c4f9adf50..1e847c9507 100644
--- a/lib/weblib.php
+++ b/lib/weblib.php
@@ -247,7 +247,8 @@ function frmchecked(&$var, $true_value = "checked", $false_value = "") {
 }
 
 
-function link_to_popup_window ($url, $name="popup", $linkname="click here", $height=400, $width=500, $title="Popup window") {
+function link_to_popup_window ($url, $name="popup", $linkname="click here", 
+                               $height=400, $width=500, $title="Popup window") {
 /// This will create a HTML link that will work on both 
 /// Javascript and non-javascript browsers.
 /// Relies on the Javascript function openpopup in javascript.php
@@ -255,21 +256,29 @@ function link_to_popup_window ($url, $name="popup", $linkname="click here", $hei
 
     global $CFG;
 
-    echo "\n<script language=\"javascript\">";
-    echo "\n<!--";
-    echo "\ndocument.write('<a title=\"".addslashes($title)."\" href=javascript:openpopup(\"$url\",\"$name\",\"$height\",\"$width\") >".addslashes($linkname)."</a>');";
-    echo "\n//-->";
-    echo "\n</script>";
-    echo "\n<noscript>\n<a target=\"$name\" title=\"$title\" href=\"$CFG->wwwroot/$url\">$linkname</a>\n</noscript>\n";
+    $options = "menubar=0,location=0,scrollbars,resizable,width=$width,height=$height";
+    $fullscreen = 0;
 
+    echo "<a target=\"$name\" title=\"$title\" href=\"$CFG->wwwroot/$url\" ".
+         "onClick=\"return openpopup('$url', '$name', '$options', $fullscreen);\">$linkname</a>\n";
 }
 
+
 function close_window_button() {
 /// Prints a simple button to close a window
 
-    echo "<form><center>";
-    echo "<input type=button onClick=\"self.close();\" value=\"".get_string("closewindow")."\">";
-    echo "</center></form>";
+    echo "<center>\n";
+    echo "<script>\n";
+    echo "<!--\n";
+    echo "document.write('<form>');\n";
+    echo "document.write('<input type=button onClick=\"self.close();\" value=\"".get_string("closewindow")."\">');\n";
+    echo "document.write('</form>');\n";
+    echo "-->\n";
+    echo "</script>\n";
+    echo "<noscript>\n";
+    echo "<a href=\"".$_SERVER['HTTP_REFERER']."\"><---</a>\n";
+    echo "</noscript>\n";
+    echo "</center>\n";
 }
 
 
diff --git a/mod/resource/details.php b/mod/resource/details.php
index 41d4a7686f..c044979f57 100644
--- a/mod/resource/details.php
+++ b/mod/resource/details.php
@@ -73,8 +73,6 @@
                 break;
 
             case WEBPAGE:
-            case WEBLINK:
-            case PROGRAM:
                 $strexampleurl = get_string("exampleurl", "resource");
                 ?>
                 <tr valign="top">
@@ -85,12 +83,117 @@
                         <input name="reference" size="100" value="<? p($form->reference) ?>">
                     </td>
                 </tr>
+                <tr valign="top">
+                    <td align="right" nowrap>&nbsp;
+                    </td>
+                    <td>
+                        <p><?php echo "($strexample) $strexampleurl" ?></p>
+                    </td>
+                </tr>
+
+                <?
+                break;
+
+            case WEBLINK:
+
+                $strexampleurl    = get_string("exampleurl", "resource");
+                $strnewwindow     = get_string("newwindow", "resource");
+                $strnewwindowopen = get_string("newwindowopen", "resource");
+
+                foreach ($RESOURCE_WINDOW_OPTIONS as $optionname) {
+                    $stringname = "str$optionname";
+                    $$stringname = get_string("new$optionname", "resource");
+                    $window->$optionname = "";
+                    $jsoption[] = "\"$optionname\"";
+                }
+                $alljsoptions = implode(",", $jsoption);
+
+                if ($form->instance) {     // Re-editing
+                    if (!$form->alltext) {
+                        $newwindow = "";
+                    } else {
+                        $newwindow = "checked";
+                        $rawoptions = explode(',', $form->alltext); 
+                        foreach ($rawoptions as $rawoption) {
+                            $option = explode('=', trim($rawoption));
+                            $optionname = $option[0];
+                            $optionvalue = $option[1];
+                            if ($optionname == "height" or $optionname == "width") {
+                                $window->$optionname = $optionvalue;
+                            } else if ($optionvalue) {
+                                $window->$optionname = "checked";
+                            }
+                        }
+                    }
+                } else {
+                    $newwindow = "checked";
+                    $window->resizable = "checked";
+                    $window->scrollbars = "checked";
+                    $window->status = "checked";
+                    $window->location = "checked";
+                    $window->width = 620;
+                    $window->height = 450;
+                }
+
+                echo $alloptions;
+
+                ?>
+
                 <tr valign="top">
                     <td align="right" nowrap>
-                        <p><b>(<?=$strexample?>)</b></p>
+                        <p><b><?php p($strtypename) ?>:</b></p>
+                    </td>
+                    <td>
+                        <input name="reference" size="100" value="<?php p($form->reference) ?>">
+                    </td>
+                </tr>
+                <tr valign="top">
+                    <td align="right" nowrap>&nbsp;
+                    </td>
+                    <td>
+                        <p><?php echo "($strexample) $strexampleurl" ?></p>
+                    </td>
+                </tr>
+                <tr valign="top">
+                    <td align="right" nowrap>
+                        <p><b><?php p($strnewwindow) ?></b></p>
                     </td>
                     <td>
-                    <p><?=$strexampleurl?>
+                      <script>
+                          var subitems = [<?php echo $alljsoptions; ?>];
+                      </script>
+                      <input name="setnewwindow" type=hidden value=1>
+                      <input name="newwindow" type=checkbox value=1 <?php p($newwindow) ?> 
+                        onclick="return lockoptions('theform','newwindow', subitems)"> 
+                      <?php p($strnewwindowopen) ?>
+                    <ul>
+                      <?php
+                          foreach ($window as $name => $value) {
+                              if ($name == "height" or $name == "width") {
+                                  continue;
+                              }
+                              echo "<input name=\"h$name\" type=hidden value=0>";
+                              echo "<input name=\"$name\" type=checkbox value=1 ".$window->$name.">";
+                              $stringname = "str$name";
+                              echo $$stringname."<br />";
+                          }
+                      ?>
+
+                      <input name="hwidth" type=hidden value=0>
+                      <input name="width" type=text size=4 value="<?php p($window->width) ?>">
+                        <?php p($strwidth) ?><br />
+
+                      <input name="hheight" type=hidden value=0>
+                      <input name="height" type=text size=4 value="<?php p($window->height) ?>">
+                        <?php p($strheight) ?><br />
+                      <?php
+                        if (!$newwindow) {
+                            echo "<script>";
+                            echo "lockoptions('theform','newwindow', subitems);";
+                            echo "</script>";
+                        }
+                      ?>
+                    </ul>
                     </p>
                     </td>
                 </tr>
@@ -98,6 +201,28 @@
                 <?
                 break;
 
+            case PROGRAM:
+                $strexampleurl = get_string("exampleurl", "resource");
+                ?>
+                <tr valign="top">
+                    <td align="right" nowrap>
+                        <p><b><?=$strtypename?>:</b></p>
+                    </td>
+                    <td>
+                        <input name="reference" size="100" value="<? p($form->reference) ?>">
+                    </td>
+                </tr>
+                <tr valign="top">
+                    <td align="right" nowrap>&nbsp;
+                    </td>
+                    <td>
+                        <p><?php echo "($strexample) $strexampleurl" ?></p>
+                    </td>
+                </tr>
+
+                <?
+                break;
+
             case UPLOADEDFILE:
                 $strfilename = get_string("filename", "resource");
                 $strnote     = get_string("note", "resource");
diff --git a/mod/resource/lib.php b/mod/resource/lib.php
index c72f4e49ca..680cbcddd4 100644
--- a/mod/resource/lib.php
+++ b/mod/resource/lib.php
@@ -20,6 +20,8 @@ $RESOURCE_TYPE = array (REFERENCE    => get_string("resourcetype1", "resource"),
 
 $RESOURCE_FRAME_SIZE = 130;
 
+$RESOURCE_WINDOW_OPTIONS = array("resizable", "scrollbars", "directories", "location", 
+                                 "menubar", "toolbar", "status", "height", "width");
 
 function resource_add_instance($resource) {
 // Given an object containing all the necessary data, 
@@ -27,8 +29,20 @@ function resource_add_instance($resource) {
 // will create a new instance and return the id number 
 // of the new instance.
 
+    global $RESOURCE_WINDOW_OPTIONS;
+
     $resource->timemodified = time();
 
+    if (isset($resource->setnewwindow)) {
+        $optionlist = array();
+        foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
+            if (isset($resource->$option)) {
+                $optionlist[] = $option."=".$resource->$option;
+            }
+        }
+        $resource->alltext = implode(',', $optionlist);
+    }
+
     return insert_record("resource", $resource);
 }
 
@@ -38,9 +52,21 @@ function resource_update_instance($resource) {
 // (defined by the form in mod.html) this function 
 // will update an existing instance with new data.
 
+    global $RESOURCE_WINDOW_OPTIONS;
+
     $resource->id = $resource->instance;
     $resource->timemodified = time();
 
+    if (isset($resource->setnewwindow)) {
+        $optionlist = array();
+        foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
+            if (isset($resource->$option)) {
+                $optionlist[] = $option."=".$resource->$option;
+            }
+        }
+        $resource->alltext = implode(',', $optionlist);
+    }
+
     return update_record("resource", $resource);
 }
 
diff --git a/mod/resource/version.php b/mod/resource/version.php
index a843367188..a3b4241f86 100644
--- a/mod/resource/version.php
+++ b/mod/resource/version.php
@@ -5,7 +5,7 @@
 //  This fragment is called by /admin/index.php
 ////////////////////////////////////////////////////////////////////////////////
 
-$module->version  = 2002122300;
+$module->version  = 2003072000;
 $module->cron     = 0;
 
 ?>
-- 
2.39.5