]> git.mjollnir.org Git - moodle.git/commitdiff
fix unchecked foreach loops
authordiml <diml>
Tue, 20 Nov 2007 23:20:22 +0000 (23:20 +0000)
committerdiml <diml>
Tue, 20 Nov 2007 23:20:22 +0000 (23:20 +0000)
search/documents/resource_document.php
search/documents/techproject_document.php

index fe2ca3bce91387f3cd2e66456b1abba04199f683..95698a08504aa412b75a290ed38d19b5a37db6bf 100644 (file)
 *
 * Functions for iterating and retrieving the necessary records are now also included
 * in this file, rather than mod/resource/lib.php
-*
-* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
-* @package search
-* @version 2007110400
 **/
 
 require_once("$CFG->dirroot/search/documents/document.php");
@@ -96,12 +92,10 @@ function resource_get_content_for_index(&$notneeded) {
     foreach($resources as $aResource){
         $coursemodule = get_field('modules', 'id', 'name', 'resource');
         $cm = get_record('course_modules', 'course', $aResource->course, 'module', $coursemodule, 'instance', $aResource->id);
-        if ($cm){
-            $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-            $aResource->id = $cm->id;
-            $documents[] = new ResourceSearchDocument(get_object_vars($aResource), $context->id);
-            mtrace("finished $aResource->name");
-        }
+        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+        $aResource->id = $cm->id;
+        $documents[] = new ResourceSearchDocument(get_object_vars($aResource), $context->id);
+        mtrace("finished $aResource->name");
     }
 
     // special physical files handling
@@ -136,12 +130,14 @@ function resource_get_content_for_index(&$notneeded) {
         $resources = get_records_sql($query);
         
         // invokes external content extractor if exists.
-        foreach($resources as $aResource){
-            // fetches a physical indexable document and adds it to documents passed by ref
-            $coursemodule = get_field('modules', 'id', 'name', 'resource');
-            $cm = get_record('course_modules', 'id', $aResource->id);
-            $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-            resource_get_physical_file($aResource, $context->id, false, $documents);
+        if ($resources){
+            foreach($resources as $aResource){
+                // fetches a physical indexable document and adds it to documents passed by ref
+                $coursemodule = get_field('modules', 'id', 'name', 'resource');
+                $cm = get_record('course_modules', 'id', $aResource->id);
+                $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+                resource_get_physical_file($aResource, $context->id, false, $documents);
+            }
         }
     }
     return $documents;
@@ -300,14 +296,11 @@ function resource_check_text_access($path, $itemtype, $this_id, $user, $group_id
     include_once("{$CFG->dirroot}/{$path}/lib.php");
     
     $r = get_record('resource', 'id', $this_id);
-    $context = get_record('context', 'id', $context_id);
-    $cm = get_record('course_modules', 'id', $context->instanceid);
-    // $cm = get_coursemodule_from_instance('resource', $r->id, $r->course);
-    // $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+    $module_context = get_record('context', 'id', $context_id);
+    $cm = get_record('course_modules', 'id', $module_context->instanceid);
 
     //check if found course module is visible
-    if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)){
-        if (!empty($CFG->search_access_debug)) echo "search reject : hidden resource ";
+    if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $module_context)){
         return false;
     }
     
index 529c948a62cc4d77bd7b6b0905361c9d244f3f24..2eeb30de728300d3f7bd3f983f5757b644b20539 100644 (file)
@@ -5,10 +5,6 @@
 * 2007/08/02
 *
 * document handling for techproject activity module
-*
-* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
-* @package search
-* @version 2007110400
 */
 /* see wiki_document.php for descriptions */
 
@@ -83,27 +79,29 @@ function techproject_get_content_for_index(&$techproject) {
     $cm = get_record('course_modules', 'course', $techproject->course, 'module', $coursemodule, 'instance', $techproject->id);
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 
-    $entries = array_merge($requirements, $specifications, $milestones, $deliverables);
-    foreach($entries as $anEntry) {
-        if ($anEntry) {
-            if (strlen($anEntry->description) > 0) {
-                $anEntry->author = '';
-                $documents[] = new TechprojectEntrySearchDocument(get_object_vars($anEntry), $techproject->course, $context->id);
+    $entries = @array_merge($requirements, $specifications, $milestones, $deliverables);
+    if ($entries){
+        foreach($entries as $anEntry) {
+            if ($anEntry) {
+                if (strlen($anEntry->description) > 0) {
+                    $documents[] = new TechprojectEntrySearchDocument(get_object_vars($anEntry), $techproject->course, $context->id);
+                } 
             } 
         } 
-    } 
-
-    foreach($tasks as $aTask) {
-        if ($aTask) {
-            if (strlen($aTask->description) > 0) {
-                if ($aTask->assignee){
-                    $user = get_record('user', 'id', $aTask->assignee);
-                    $aTask->author = fullname($user);
-                }
-                $documents[] = new TechprojectEntrySearchDocument(get_object_vars($aTask), $techproject->course, $context->id);
+    }
+    if ($tasks){
+        foreach($tasks as $aTask) {
+            if ($aTask) {
+                if (strlen($aTask->description) > 0) {
+                    if ($aTask->assignee){
+                        $user = get_record('user', 'id', $aTask->assignee);
+                        $aTask->author = $user->firstname.' '.$user->lastname;
+                    }
+                    $documents[] = new TechprojectEntrySearchDocument(get_object_vars($aTask), $techproject->course, $context->id);
+                } 
             } 
         } 
-    } 
+    }
     return $documents;
 } //techproject_get_content_for_index
 
@@ -141,14 +139,10 @@ function techproject_single_document($id, $itemtype) {
     $techprojet_course = get_field('techproject', 'course', 'id', $entry->projectid);
     $coursemodule = get_field('modules', 'id', 'name', 'techproject');
     $cm = get_record('course_modules', 'course', $techproject_course, 'module', $coursemodule, 'instance', $entry->projectid);
-    if ($cm){
-        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-        $entry->type = $itemtype;
-        $techproject = get_record('techproject', 'id', $requirement->projectid);
-        return new TechprojectEntrySearchDocument(get_object_vars($anEntry), $techproject->course, $context->id);    
-    }
-    return null;
-    
+    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+    $entry->type = $itemtype;
+    $techproject = get_record('techproject', 'id', $requirement->projectid);
+    return new TechprojectEntrySearchDocument(get_object_vars($anEntry), $techproject->course, $context->id);
 } //techproject_single_document
 
 /**
@@ -259,42 +253,27 @@ function techproject_check_text_access($path, $entry_type, $this_id, $user, $gro
 
     // get the techproject object and all related stuff
     $techproject = get_record('techproject', 'id', $this_id);
-    $context = get_record('context', 'id', $context_id);
-    $cm = get_record('course_modules', 'id', $context->instanceid);
-    // $cm = get_coursemodule_from_instance('techproject', $techproject->id, $techproject->course);
-    // $context = get_record('context', 'id', $cm->id);
-    if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) return false;
+    $course = get_record('course', 'id', $techproject->course);
+    $module_context = get_record('context', 'id', $context_id);
+    $cm = get_record('course_modules', 'id', $module_context->instanceid);
+    if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $module_context)) return false;
     
     //group consistency check : checks the following situations about groups
     // if user is guest check access capabilities for guests :
     // guests can see default project, and other records if groups are liberal
     // TODO : change guestsallowed in a capability
-    $course = get_record('course', 'id', $techproject->course);
     if (isguest() && $techproject->guestsallowed){
-        if ($group_id && groupmode($course, $cm) == SEPARATEGROUPS){
-            if (!empty($CFG->search_access_debug)) echo "search reject : no group access for guests ";
+        if ($group_id && groupmode($course, $cm) == SEPARATEGROUPS)
             return false;
-        }
         return true;
     }
     
     // trap if user is not same group and groups are separated
     $current_group = get_current_group($course->id);
-    if ((groupmode($course, $cm) == SEPARATEGROUPS) && $group_id != $current_group && $group_id) {
-        if (!empty($CFG->search_access_debug)) echo "search reject : resource not in my group when separated ";
-        return false;
-    }
+    if ((groupmode($course) == SEPARATEGROUPS) && $group_id != $current_group && $group_id) return false;
     
     //trap if ungroupedsees is off in strict access mode and user is not teacher
-    if ((groupmode($course, $cm) == SEPARATEGROUPS) && !$techproject->ungroupedsees && !$group_id && isteacher($user->id)){
-        if (!empty($CFG->search_access_debug)) echo "search reject : ungrouped cannot see anything here, unless teacher ";
-        return false;
-    }
-
-    //opening periods check
-    // trap if user has not capability to see hidden records and date is out of opening range
-    // TODO : find a strategy
-    $now = usertime(time());
+    if ((groupmode($course) == SEPARATEGROUPS) && !$techproject->ungroupedsees && !$group_id && isteacher($user->id)) return false;
     
     return true;
 } //techproject_check_text_access