MDL-49231 mod_glossary: Update view and print to use new lib functions
authorFrederic Massart <fred@moodle.com>
Mon, 19 Oct 2015 06:37:50 +0000 (14:37 +0800)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Thu, 31 Dec 2015 14:18:39 +0000 (15:18 +0100)
mod/glossary/print.php
mod/glossary/sql.php
mod/glossary/view.php

index e6f4b9d..145cabc 100644 (file)
@@ -187,26 +187,30 @@ echo html_writer::tag('div', $modname, array('class' => 'modname'));
 if ( $allentries ) {
     foreach ($allentries as $entry) {
 
 if ( $allentries ) {
     foreach ($allentries as $entry) {
 
-        // Setting the pivot for the current entry
-        $pivot = $entry->glossarypivot;
-        $upperpivot = core_text::strtoupper($pivot);
-        $pivottoshow = core_text::strtoupper(format_string($pivot, true, $fmtoptions));
-        // Reduce pivot to 1cc if necessary
-        if ( !$fullpivot ) {
-            $upperpivot = core_text::substr($upperpivot, 0, 1);
-            $pivottoshow = core_text::substr($pivottoshow, 0, 1);
-        }
+        // Setting the pivot for the current entry.
+        if ($printpivot) {
+
+           if ($userispivot) {
+                $pivot = $entry->userid;
+            } else {
+                $pivot = $pivotfn($entry);
+            }
+            $upperpivot = core_text::strtoupper($pivot);
+            $pivottoshow = core_text::strtoupper(format_string($pivot, true, $fmtoptions));
 
 
-        // If there's  group break
-        if ( $currentpivot != $upperpivot ) {
+            // Reduce pivot to 1cc if necessary.
+            if (!$fullpivot) {
+                $upperpivot = core_text::substr($upperpivot, 0, 1);
+                $pivottoshow = core_text::substr($pivottoshow, 0, 1);
+            }
 
 
-            // print the group break if apply
-            if ( $printpivot )  {
+            // If there's a group break.
+            if ($currentpivot != $upperpivot) {
                 $currentpivot = $upperpivot;
 
                 $currentpivot = $upperpivot;
 
-                if ( isset($entry->userispivot) ) {
-                    // printing the user icon if defined (only when browsing authors)
-                    $user = $DB->get_record("user", array("id"=>$entry->userid));
+                if ($userispivot) {
+                    // Printing the user icon if defined (only when browsing authors).
+                    $user = mod_glossary_entry_query_builder::get_user_from_record($entry);
                     $pivottoshow = fullname($user);
                 }
                 echo html_writer::tag('div', clean_text($pivottoshow), array('class' => 'mdl-align strong'));
                     $pivottoshow = fullname($user);
                 }
                 echo html_writer::tag('div', clean_text($pivottoshow), array('class' => 'mdl-align strong'));
index 93e9e20..7e6a0c5 100644 (file)
  * @copyright 2003
  **/
 
  * @copyright 2003
  **/
 
-/// Creating the SQL statements
-
-/// Initialise some variables
-$sqlorderby = '';
-$sqlsortkey = NULL;
+/**
+ * This file defines, or redefines, the following variables:
+ *
+ * bool $userispivot Whether the user is the pivot.
+ * bool $fullpivot Whether the pivot should be displayed in full.
+ * bool $printpivot Whether the pivot should be displayed.
+ * string $pivotkey The property of the record at which the pivot is.
+ * int $count The number of records matching the request.
+ * array $allentries The entries matching the request.
+ * mixed $field Unset in this file.
+ * mixed $entry Unset in this file.
+ * mixed $canapprove Unset in this file.
+ *
+ * It relies on the following variables:
+ *
+ * object $glossary The glossary object.
+ * context $context The glossary context.
+ * mixed $hook The hook for the selected tab.
+ * string $sortkey The key to sort the records.
+ * string $sortorder The order of the sorting.
+ * int $offset The number of records to skip.
+ * int $entriesbypage The number of entries per page.
+ * string $mode The mode of browsing.
+ * string $tab The tab selected.
+ */
+
+$userispivot = false;
+$fullpivot = true;
+$pivotkey = 'concept';
 
 
-// For cases needing inner view
-$sqlwrapheader = '';
-$sqlwrapfooter = '';
+switch ($tab) {
 
 
-/// Calculate the SQL sortkey to be used by the SQL statements later
-switch ( $sortkey ) {
-    case "CREATION":
-        $sqlsortkey = "timecreated";
-        break;
-    case "UPDATE":
-        $sqlsortkey = "timemodified";
-        break;
-    case "FIRSTNAME":
-        $sqlsortkey = "firstname";
-        break;
-    case "LASTNAME":
-        $sqlsortkey = "lastname";
+    case GLOSSARY_AUTHOR_VIEW:
+        $userispivot = true;
+        $pivotkey = 'userid';
+        $field = ($sortkey == 'LASTNAME' ? 'LASTNAME' : 'FIRSTNAME');
+        list($allentries, $count) = glossary_get_entries_by_author($glossary, $context, $hook,
+            $field, $sortorder, $offset, $entriesbypage);
+        unset($field);
         break;
         break;
-}
-$sqlsortorder = $sortorder;
-
-/// Pivot is the field that set the break by groups (category, initial, author name, etc)
-
-/// fullpivot indicate if the whole pivot should be compared agasint the db or just the first letter
-/// printpivot indicate if the pivot should be printed or not
 
 
-$fullpivot = 1;
-$params = array('gid1'=>$glossary->id, 'gid2'=>$glossary->id, 'myid'=>$USER->id, 'hook'=>$hook);
-
-$userid = '';
-if ( isloggedin() ) {
-    $userid = "OR ge.userid = :myid";
-}
-switch ($tab) {
     case GLOSSARY_CATEGORY_VIEW:
     case GLOSSARY_CATEGORY_VIEW:
-        if ($hook == GLOSSARY_SHOW_ALL_CATEGORIES  ) {
-
-            $sqlselect = "SELECT gec.id AS cid, ge.*, gec.entryid, gc.name AS glossarypivot";
-            $sqlfrom   = "FROM {glossary_entries} ge,
-                               {glossary_entries_categories} gec,
-                               {glossary_categories} gc";
-            $sqlwhere  = "WHERE (ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2) AND
-                          ge.id = gec.entryid AND gc.id = gec.categoryid AND
-                          (ge.approved <> 0 $userid)";
-
-            $sqlorderby = ' ORDER BY gc.name, ge.concept';
-
-        } elseif ($hook == GLOSSARY_SHOW_NOT_CATEGORISED ) {
-
-            $printpivot = 0;
-            $sqlselect = "SELECT ge.*, concept AS glossarypivot";
-            $sqlfrom   = "FROM {glossary_entries} ge LEFT JOIN {glossary_entries_categories} gec
-                               ON ge.id = gec.entryid";
-            $sqlwhere  = "WHERE (glossaryid = :gid1 OR sourceglossaryid = :gid2) AND
-                          (ge.approved <> 0 $userid) AND gec.entryid IS NULL";
-
-
-            $sqlorderby = ' ORDER BY concept';
-
-        } else {
-
-            $printpivot = 0;
-            $sqlselect  = "SELECT ge.*, ce.entryid, c.name AS glossarypivot";
-            $sqlfrom    = "FROM {glossary_entries} ge, {glossary_entries_categories} ce, {glossary_categories} c";
-            $sqlwhere   = "WHERE ge.id = ce.entryid AND ce.categoryid = :hook AND
-                                 ce.categoryid = c.id AND ge.approved != 0 AND
-                                 (ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2) AND
-                          (ge.approved <> 0 $userid)";
-
-            $sqlorderby = ' ORDER BY c.name, ge.concept';
-
+        $hook = (int) $hook; // Make sure it's properly casted to int.
+        list($allentries, $count) = glossary_get_entries_by_category($glossary, $context, $hook, $offset, $entriesbypage);
+        $pivotkey = 'categoryname';
+        if ($hook != GLOSSARY_SHOW_ALL_CATEGORIES) {
+            $printpivot = false;
         }
         break;
         }
         break;
-    case GLOSSARY_AUTHOR_VIEW:
-
-        $where = '';
-        $params['hookup'] = core_text::strtoupper($hook);
-
-        if ( $sqlsortkey == 'firstname' ) {
-            $usernamefield = $DB->sql_fullname('u.firstname' , 'u.lastname');
-        } else {
-            $usernamefield = $DB->sql_fullname('u.lastname' , 'u.firstname');
-        }
-        if ($hook != 'ALL' && ($hookstrlen = core_text::strlen($hook))) {
-            $where = "AND " . $DB->sql_substr("upper($usernamefield)", 1, core_text::strlen($hook)) . " = :hookup";
-        }
 
 
-        $sqlselect  = "SELECT ge.*, $usernamefield AS glossarypivot, 1 AS userispivot ";
-        $sqlfrom    = "FROM {glossary_entries} ge, {user} u";
-        $sqlwhere   = "WHERE ge.userid = u.id  AND
-                             (ge.approved <> 0 $userid)
-                             $where AND
-                             (ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2)";
-        $sqlorderby = "ORDER BY $usernamefield $sqlsortorder, ge.concept";
+    case GLOSSARY_DATE_VIEW:
+        $printpivot = false;
+        $field = ($sortkey == 'CREATION' ? 'CREATION' : 'UPDATE');
+        list($allentries, $count) = glossary_get_entries_by_date($glossary, $context, $field, $sortorder,
+            $offset, $entriesbypage);
+        unset($field);
         break;
         break;
-    case GLOSSARY_APPROVAL_VIEW:
-        $fullpivot = 0;
-        $printpivot = 0;
-
-        $where = '';
-        $params['hookup'] = core_text::strtoupper($hook);
-
-        if ($hook != 'ALL' and $hook != 'SPECIAL' && ($hookstrlen = core_text::strlen($hook))) {
-            $where = "AND " . $DB->sql_substr("upper(concept)", 1, $hookstrlen) . " = :hookup";
-        }
 
 
-        $sqlselect  = "SELECT ge.*, ge.concept AS glossarypivot";
-        $sqlfrom    = "FROM {glossary_entries} ge";
-        $sqlwhere   = "WHERE (ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2) AND
-                             ge.approved = 0 $where";
-
-        if ( $sqlsortkey ) {
-            $sqlorderby = "ORDER BY $sqlsortkey $sqlsortorder";
-        } else {
-            $sqlorderby = "ORDER BY ge.concept";
-        }
+    case GLOSSARY_APPROVAL_VIEW:
+        $fullpivot = false;
+        $printpivot = false;
+        list($allentries, $count) = glossary_get_entries_to_approve($glossary, $context, $hook, $sortkey, $sortorder,
+            $offset, $entriesbypage);
         break;
         break;
-    case GLOSSARY_DATE_VIEW:
-        $printpivot = 0;
+
     case GLOSSARY_STANDARD_VIEW:
     default:
     case GLOSSARY_STANDARD_VIEW:
     default:
-        $sqlselect  = "SELECT ge.*, ge.concept AS glossarypivot";
-        $sqlfrom    = "FROM {glossary_entries} ge";
-
-        $where = '';
-        $fullpivot = 0;
-
-        switch ( $mode ) {
+        $fullpivot = false;
+        switch ($mode) {
             case 'search':
             case 'search':
-
-                if ($DB->sql_regex_supported()) {
-                    $REGEXP    = $DB->sql_regex(true);
-                    $NOTREGEXP = $DB->sql_regex(false);
-                }
-
-                $searchcond = array();
-                $alcond     = array();
-                //$params     = array();
-                $i = 0;
-
-                $searchterms = explode(" ",$hook);
-
-                foreach ($searchterms as $searchterm) {
-                    $i++;
-
-                    $NOT = false; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle
-                               /// will use it to simulate the "-" operator with LIKE clause
-
-                    if (empty($fullsearch)) {
-                        // With fullsearch disabled, look only within concepts and aliases.
-                        $concat = $DB->sql_concat('ge.concept', "' '", "COALESCE(al.alias, :emptychar".$i.")");
-                    } else {
-                        // With fullsearch enabled, look also within definitions.
-                        $concat = $DB->sql_concat('ge.concept', "' '", 'ge.definition', "' '", "COALESCE(al.alias, :emptychar".$i.")");
-                    }
-                    $params['emptychar'.$i] = '';
-
-                    /// Under Oracle and MSSQL, trim the + and - operators and perform
-                    /// simpler LIKE (or NOT LIKE) queries
-                    if (!$DB->sql_regex_supported()) {
-                        if (substr($searchterm, 0, 1) == '-') {
-                            $NOT = true;
-                        }
-                        $searchterm = trim($searchterm, '+-');
-                    }
-
-                    if (substr($searchterm,0,1) == '+') {
-                        $searchterm = trim($searchterm, '+-');
-                        if (core_text::strlen($searchterm) < 2) {
-                            continue;
-                        }
-                        $searchterm = preg_quote($searchterm, '|');
-                        $searchcond[] = "$concat $REGEXP :ss$i";
-                        $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)";
-
-                    } else if (substr($searchterm,0,1) == "-") {
-                        $searchterm = trim($searchterm, '+-');
-                        if (core_text::strlen($searchterm) < 2) {
-                            continue;
-                        }
-                        $searchterm = preg_quote($searchterm, '|');
-                        $searchcond[] = "$concat $NOTREGEXP :ss$i";
-                        $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)";
-
-                    } else {
-                        if (core_text::strlen($searchterm) < 2) {
-                            continue;
-                        }
-                        $searchcond[] = $DB->sql_like($concat, ":ss$i", false, true, $NOT);
-                        $params['ss'.$i] = "%$searchterm%";
-                    }
-                }
-
-                if (empty($searchcond)) {
-                    $where = "AND 1=2 "; // no search result
-
-                } else {
-                    $searchcond = implode(" AND ", $searchcond);
-
-                    // Need one inner view here to avoid distinct + text
-                    $sqlwrapheader = 'SELECT ge.*, ge.concept AS glossarypivot
-                                        FROM {glossary_entries} ge
-                                        JOIN ( ';
-                    $sqlwrapfooter = ' ) gei ON (ge.id = gei.id)';
-
-                    $sqlselect  = "SELECT DISTINCT ge.id";
-                    $sqlfrom    = "FROM {glossary_entries} ge
-                                   LEFT JOIN {glossary_alias} al ON al.entryid = ge.id";
-                    $where      = "AND ($searchcond)";
-                }
-
+                list($allentries, $count) = glossary_get_entries_by_search($glossary, $context, $hook, $fullsearch,
+                    $sortkey, $sortorder, $offset, $entriesbypage);
                 break;
 
             case 'term':
                 break;
 
             case 'term':
-                $params['hook2'] = $hook;
-                $printpivot = 0;
-                $sqlfrom .= " LEFT JOIN {glossary_alias} ga on ge.id = ga.entryid";
-                $where = "AND (ge.concept = :hook OR ga.alias = :hook2) ";
+                $printpivot = false;
+                list($allentries, $count) = glossary_get_entries_by_term($glossary, $context, $hook, $offset, $entriesbypage);
                 break;
 
             case 'entry':
                 break;
 
             case 'entry':
-                $printpivot = 0;
-                $where = "AND ge.id = :hook";
-                break;
-
-            case 'letter':
-                if ($hook != 'ALL' and $hook != 'SPECIAL' and ($hookstrlen = core_text::strlen($hook))) {
-                    $params['hookup'] = core_text::strtoupper($hook);
-                    $where = "AND " . $DB->sql_substr("upper(concept)", 1, $hookstrlen) . " = :hookup";
-                }
-                if ($hook == 'SPECIAL') {
-                    //Create appropiate IN contents
-                    $alphabet = explode(",", get_string('alphabet', 'langconfig'));
-                    list($nia, $aparams) = $DB->get_in_or_equal($alphabet, SQL_PARAMS_NAMED, $start='a', false);
-                    $params = array_merge($params, $aparams);
-                    $where = "AND " . $DB->sql_substr("upper(concept)", 1, 1) . " $nia";
+                $printpivot = false;
+                $entry = glossary_get_entry_by_id($hook);
+                $canapprove = has_capability('mod/glossary:approve', $context);
+                if ($entry && ($entry->glossaryid == $glossary->id || $entry->sourceglossaryid != $glossary->id)
+                        && (!empty($entry->approved) || $entry->userid == $USER->id || $canapprove)) {
+                    $count = 1;
+                    $allentries = array($entry);
+                } else {
+                    $count = 0;
+                    $allentries = array();
                 }
                 }
-                break;
-        }
-
-        $sqlwhere   = "WHERE (ge.glossaryid = :gid1 or ge.sourceglossaryid = :gid2) AND
-                             (ge.approved <> 0 $userid)
-                              $where";
-        switch ( $tab ) {
-            case GLOSSARY_DATE_VIEW:
-                $sqlorderby = "ORDER BY $sqlsortkey $sqlsortorder";
+                unset($entry, $canapprove);
                 break;
 
                 break;
 
-            case GLOSSARY_STANDARD_VIEW:
-                $sqlorderby = "ORDER BY ge.concept";
+            case 'letter':
             default:
             default:
+                list($allentries, $count) = glossary_get_entries_by_letter($glossary, $context, $hook, $offset, $entriesbypage);
                 break;
         }
         break;
 }
                 break;
         }
         break;
 }
-
-$count = 0;
-if ($tab == GLOSSARY_CATEGORY_VIEW && $hook == GLOSSARY_SHOW_ALL_CATEGORIES) {
-    $count = $DB->count_records_sql("SELECT COUNT(ge.id) $sqlfrom $sqlwhere", $params);
-} else {
-    $count = $DB->count_records_sql("SELECT COUNT(DISTINCT(ge.id)) $sqlfrom $sqlwhere", $params);
-}
-
-$limitfrom = $offset;
-$limitnum = 0;
-
-if ( $offset >= 0 ) {
-    $limitnum = $entriesbypage;
-}
-
-$query = "$sqlwrapheader $sqlselect $sqlfrom $sqlwhere $sqlwrapfooter $sqlorderby";
-$allentries = $DB->get_records_sql($query, $params, $limitfrom, $limitnum);
\ No newline at end of file
index 2c8019d..178769b 100644 (file)
@@ -447,31 +447,31 @@ if ($allentries) {
     foreach ($allentries as $entry) {
 
         // Setting the pivot for the current entry
     foreach ($allentries as $entry) {
 
         // Setting the pivot for the current entry
-        $pivot = $entry->glossarypivot;
-        $upperpivot = core_text::strtoupper($pivot);
-        $pivottoshow = core_text::strtoupper(format_string($pivot, true, $fmtoptions));
-        // Reduce pivot to 1cc if necessary
-        if ( !$fullpivot ) {
-            $upperpivot = core_text::substr($upperpivot, 0, 1);
-            $pivottoshow = core_text::substr($pivottoshow, 0, 1);
-        }
-
-        // if there's a group break
-        if ( $currentpivot != $upperpivot ) {
+        if ($printpivot) {
+            $pivot = $entry->{$pivotkey};
+            $upperpivot = core_text::strtoupper($pivot);
+            $pivottoshow = core_text::strtoupper(format_string($pivot, true, $fmtoptions));
+
+            // Reduce pivot to 1cc if necessary.
+            if (!$fullpivot) {
+                $upperpivot = core_text::substr($upperpivot, 0, 1);
+                $pivottoshow = core_text::substr($pivottoshow, 0, 1);
+            }
 
 
-            // print the group break if apply
-            if ( $printpivot )  {
+            // If there's a group break.
+            if ($currentpivot != $upperpivot) {
                 $currentpivot = $upperpivot;
 
                 $currentpivot = $upperpivot;
 
+                // print the group break if apply
+
                 echo '<div>';
                 echo '<table cellspacing="0" class="glossarycategoryheader">';
 
                 echo '<tr>';
                 echo '<div>';
                 echo '<table cellspacing="0" class="glossarycategoryheader">';
 
                 echo '<tr>';
-                if ( isset($entry->userispivot) ) {
+                if ($userispivot) {
                 // printing the user icon if defined (only when browsing authors)
                     echo '<th align="left">';
                 // printing the user icon if defined (only when browsing authors)
                     echo '<th align="left">';
-
-                    $user = $DB->get_record("user", array("id"=>$entry->userid));
+                    $user = mod_glossary_entry_query_builder::get_user_from_record($entry);
                     echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
                     $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', context_course::instance($course->id)));
                 } else {
                     echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
                     $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', context_course::instance($course->id)));
                 } else {
@@ -480,7 +480,6 @@ if ($allentries) {
 
                 echo $OUTPUT->heading($pivottoshow, 3);
                 echo "</th></tr></table></div>\n";
 
                 echo $OUTPUT->heading($pivottoshow, 3);
                 echo "</th></tr></table></div>\n";
-
             }
         }
 
             }
         }