1+ <?php 
2+ 
3+ namespace  mod_moodleoverflow \search ;
4+ 
5+ use  core_search \document ;
6+ 
7+ defined ('MOODLE_INTERNAL ' ) || die ();
8+ require_once (dirname (__FILE__ ) . '/../../locallib.php ' );
9+ class  moodleoverflowposts extends  \core_search \base_mod {
10+ 
11+     /** 
12+      * @var array Internal quick static cache. 
13+      */ 
14+     protected  $ postsdata  = array ();
15+     protected  $ moodleoverflows  = array ();
16+     protected  $ discussions  = array ();
17+ 
18+     protected  static  $ levels  = [CONTEXT_COURSE ];
19+ 
20+     public  function  uses_file_indexing () {
21+         return  false ;
22+     }
23+ 
24+     public  function  get_document ($ record , $ options  = array ()) {
25+         try  {
26+             $ cm  = $ this  ->get_cm ('moodleoverflow ' , $ record ->moodleoverflowid , $ record ->course );
27+             $ context  = \context_module::instance ($ cm ->id );
28+         } catch  (\dml_missing_record_exception   $ ex ) {
29+             debugging ('Error retrieving  '  . $ this  ->areaid  . '  '  . $ record ->id  . ' document, not all required data is available:  '  .
30+                 $ ex ->getMessage (), DEBUG_DEVELOPER );
31+             return  false ;
32+         } catch  (\dml_exception   $ ex ) {
33+             debugging ('Error retrieving  '  . $ this  ->areaid  . '  '  . $ record ->id  . ' document:  '  . $ ex ->getMessage (), DEBUG_DEVELOPER );
34+             return  false ;
35+         }
36+ 
37+         $ doc  = \core_search \document_factory::instance ($ record ->id , $ this  ->componentname , $ this  ->areaname );
38+ 
39+         $ doc ->set ('title ' , content_to_text ($ record ->title , true ));
40+         $ doc ->set ('content ' , content_to_text ($ record ->message , FORMAT_HTML ));
41+         $ doc ->set ('contextid ' , $ context ->id );
42+         $ doc ->set ('type ' , \core_search \manager::TYPE_TEXT );
43+         $ doc ->set ('courseid ' , $ record ->course );
44+         $ doc ->set ('modified ' , $ record ->modified );
45+         $ doc ->set ('userid ' , $ record ->userid );
46+         $ doc ->set ('owneruserid ' , \core_search \manager::NO_OWNER_ID );
47+         $ postdata [$ record ->id [0 ]] = array ('discussionid '  => $ record ->discussion , 'moodleoverflowid '  => $ record ->moodleoverflowid );
48+         if  (isset ($ options ['lastindexedtime ' ]) && ($ options ['lastindexedtime ' ] < $ record ->created )) {
49+             // If the document was created after the last index time, it must be new. 
50+             $ doc ->set_is_new (true );
51+         }
52+ 
53+         return  $ doc ;
54+     }
55+ 
56+     private  function  get_discussion_from_id ($ discussionid ) {
57+         global  $ DB  ;
58+         if  (isset ($ this  ->discussions [$ discussionid ])) {
59+             return  $ this  ->discussions [$ discussionid ];
60+         } else  {
61+             if  (!$ discussion  = $ DB  ->get_record ('moodleoverflow_discussions ' , array ('id '  => $ discussionid ))) {
62+                 return  false ;
63+             }
64+             $ this  ->discussions [$ discussionid ] = $ discussion ;
65+             return  $ discussion ;
66+         }
67+     }
68+ 
69+     private  function  get_moodleoverflow_from_id ($ moodleoverflowid ) {
70+         global  $ DB  ;
71+         if  (isset ($ this  ->moodleoverflows [$ moodleoverflowid ])) {
72+             return  $ this  ->moodleoverflows [$ moodleoverflowid ];
73+         } else  {
74+             if  (!$ moodleoverflow  = $ DB  ->get_record ('moodleoverflow ' , array ('id '  => $ moodleoverflowid ))) {
75+                 return  false ;
76+             }
77+             $ this  ->moodleoverflows [$ moodleoverflowid ] = $ moodleoverflow ;
78+             return  $ moodleoverflow ;
79+         }
80+     }
81+ 
82+     public  function  check_access ($ id ) {
83+         try  {
84+             $ post  = moodleoverflow_get_post_full ($ id );
85+             if  (!$ discussion  = $ this  ->get_discussion_from_id ($ post ->discussion )) {
86+                 return  \core_search \manager::ACCESS_DELETED ;
87+             }
88+             if  (!$ moodleoverflow  = $ this  ->get_moodleoverflow_from_id ($ post ->moodleoverflow )) {
89+                 return  \core_search \manager::ACCESS_DELETED ;
90+             }
91+             $ context  = moodleoverflow_get_context ($ post ->moodleoverflow );
92+         } catch  (\dml_missing_record_exception   $ ex ) {
93+             return  \core_search \manager::ACCESS_DELETED ;
94+         } catch  (\dml_exception   $ ex ) {
95+             return  \core_search \manager::ACCESS_DENIED ;
96+         }
97+         if  (moodleoverflow_user_can_see_discussion ($ moodleoverflow , $ discussion , $ context )) {
98+             return  \core_search \manager::ACCESS_GRANTED ;
99+         }
100+         return  \core_search \manager::ACCESS_DENIED ;
101+     }
102+ 
103+     public  function  get_discussionid_for_document (document   $ doc ) {
104+         global  $ DB  ;
105+         $ postid  = $ doc ->get ('itemid ' );
106+         if  (isset ($ this  ->postsdata [$ postid ]) && isset ($ this  ->postsdata [$ postid ]['discussionid ' ])) {
107+             return  $ this  ->postsdata [$ postid ]['discussionid ' ];
108+         } else  {
109+             $ discussionid  = $ DB  ->get_field ('moodleoverflow_posts ' , 'discussion ' , array ('id '  => $ postid ));
110+             if  (!isset ($ this  ->postsdata [$ postid ])) {
111+                 $ this  ->postsdata [$ postid ] = array ();
112+             }
113+             $ this  ->postsdata [$ postid ]['discussionid ' ] = $ discussionid ;
114+             return  $ discussionid ;
115+         }
116+     }
117+ 
118+     public  function  get_moodleoverflowid_for_document (document   $ doc ) {
119+         global  $ DB  ;
120+         $ discussionid  = $ this  ->get_discussionid_for_document ($ doc );
121+         $ postid  = $ doc ->get ('itemid ' );
122+         if  (isset ($ this  ->postsdata [$ postid ]) && isset ($ this  ->postsdata [$ postid ]["moodleoverflowid " ])) {
123+             return  $ this  ->postsdata [$ postid ]["moodleoverflowid " ];
124+         } else  {
125+             $ moodleoverflowid  = $ DB  ->get_field ('moodleoverflow_discussions ' , 'moodleoverflow ' , array ('id '  => $ discussionid ));
126+             if  (!isset ($ this  ->postsdata [$ postid ])) {
127+                 $ this  ->postsdata [$ postid ] = array ();
128+             }
129+             $ this  ->postsdata [$ postid ]['moodleoverflowid ' ] = $ moodleoverflowid ;
130+             return  $ moodleoverflowid ;
131+         }
132+     }
133+ 
134+     public  function  get_doc_url (document   $ doc ) {
135+         return  new  \moodle_url ('/mod/moodleoverflow/discussion.php ' , array ('d '  => $ this  ->get_discussionid_for_document ($ doc )),
136+             "p "  . $ doc ->get ('itemid ' ));
137+     }
138+ 
139+     public  function  get_context_url (document   $ doc ) {
140+         return  new  \moodle_url ('/mod/moodleoverflow/view.php ' , array ('m '  => $ this  ->get_moodleoverflowid_for_document ($ doc )));
141+     }
142+ 
143+     public  function  get_document_recordset ($ modifiedfrom  = 0 , \context   $ context  = null ) {
144+         global  $ DB  ;
145+         list ($ contextjoin , $ contextparams ) = $ this  ->get_context_restriction_sql ($ context , 'moodleoverflow ' , 'discussions ' );
146+         if  ($ contextjoin  === null ) {
147+             return  null ;
148+         }
149+         $ sql  = "SELECT md.name as title, mp.*, mo.course, mo.id as moodleoverflowid FROM {moodleoverflow_posts} mp  
150+                                                         JOIN {moodleoverflow_discussions} md ON mp.discussion = md.id 
151+                                                         JOIN {moodleoverflow} mo ON md.moodleoverflow = mo.id 
152+                      $ contextjoin
153+                 WHERE mp.modified >= ? ORDER BY mp.modified ASC " ;
154+         return  $ DB  ->get_recordset_sql ($ sql , array_merge ($ contextparams , [$ modifiedfrom ]));
155+     }
156+ }
0 commit comments