lib.php 14.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * The mynotes block helper functions and callbacks
 *
 * @package    block_mynotes
 * @author     Gautam Kumar Das<gautam.arg@gmail.com>
 */

defined('MOODLE_INTERNAL') || die();


class block_mynotes_manager  {

    public $perpage = 5;
    private $config = null;

    /*
     * Constructor.
     */
    public function __construct() {
        $this->config = get_config('block_mynotes');
        $this->perpage = $this->config->mynotesperpage;
    }

    /**
     * Returns matched mynotes
     *
     * @param  int $page
     * @return array
     */
    public function get_mynotes($options) {
        global $DB, $CFG, $USER, $OUTPUT;

        $page = (!isset($options->page) || !$options->page) ? 0 : $options->page;
        $perpage = $this->perpage;

        $params = array();
        $start = $page * $perpage;
        $ufields = 'u.id';

        $where = ' m.userid = :userid';
        $params['userid'] = $USER->id;
        /* if (isset($options->contextarea) && !empty($options->contextarea)) {
            $where .= ' AND m.contextarea = :contextarea';
            $params['contextarea'] = $options->contextarea;
        }
        if (isset($options->courseid) && $options->courseid) {
            $where .= ' AND m.courseid = :courseid';
            $params['courseid'] = $options->courseid;            
        } */
        if(isset($options->courseid) && $options->courseid){
        	$where .= ' AND m.courseid = :courseid';
        	$params['courseid'] = $options->courseid;
        	
        }
        $sql = "SELECT $ufields, 
            m.id AS mynoteid, m.content AS ccontent, m.contextarea, m.contextid, m.format AS cformat, 
            m.timecreated AS timecreated,m.notestime AS notestime,m.notesurl AS notesurl, c.fullname as coursename, m.courseid
                  FROM {block_mynotes} m
                  JOIN {user} u ON u.id = m.userid
                  LEFT JOIN {course} c ON c.id = m.courseid
                 WHERE $where
              ORDER BY m.timecreated DESC"; 
        $strftime = get_string('strftimerecentfull', 'langconfig');
        $mynotes = array();
        $formatoptions = array('overflowdiv' => true);
        $start = (isset($options->limitfrom)) ? $options->limitfrom : $start;
        $rs = $DB->get_recordset_sql($sql, $params, $start, $perpage);
        foreach ($rs as $u) {
            $c = new stdClass();
            $c->id = $u->mynoteid;
            $c->userid = $u->id;
            if ($u->courseid != SITEID) {
// 				$c->coursename = html_writer::link(course_get_url($u->courseid), $u->coursename);
					if (!empty($u->notestime)){
							$c->coursename='<a href="#" class="notes_button_redirect"  name="'.$u->notestime.'" id="'.course_get_url(($u->courseid), $u->coursename).'">'.$u->coursename.'</a>';
						}else{
							$c->coursename='<a href="#" class="notes_button_redirect" id="'.course_get_url(($u->courseid), $u->coursename).'">'.$u->coursename.'</a>';
						
						}
				if (! empty ( $u->notesurl )) {
// 					$c->coursename = html_writer::link ( $u->notesurl, $u->coursename );
					if (!empty($u->notestime)){
					$c->coursename='<a href="#" class="notes_button_redirect" name="'.$u->notestime.'" id="'.$u->notesurl.'">'.$u->coursename.'</a>';
					}else{
						$c->coursename='<a href="#" class="notes_button_redirect" id="'.$u->notesurl.'">'.$u->coursename.'</a>';
					}
				} else {
					
					$c->coursename = $u->coursename;
				}
			}  else {
                $c->coursename = '';
            }
            $c->content = $u->ccontent;
            $c->contextarea = $u->contextarea;
            $c->format = $u->cformat;
            $c->timecreated = userdate($u->timecreated, $strftime);
            $c->userid = $u->id;
            if (!empty($u->notestime)) {
            	$c->notestime = $u->notestime;
            } else {
            	$c->notestime = '';
            }
            
            if (!empty($u->notesurl)) {
            	$c->notesurl = $u->notesurl;
            } else {
            	$c->notesurl = '';
            }
//             $c->notestime = $u->notestime;
            $c->content = format_text($c->content, $c->format, $formatoptions);
            $c->delete = true;
            $mynotes[] = $c;
        }
        $rs->close();
        return $mynotes;
    }
    
    /**
     * Returns count of the mynotes in a table where all the given conditions met.
     *
     * @param object $options
     * @return int The count of records
     */
   /*  public function count_mynotes($options) {
        global $DB, $USER;
        $params = array();
        $params['userid'] = $USER->id;
        
        $sql='select COUNT(contextarea) from {block_mynotes} where contextid= :contextid';
        $params ['contextid']= $options->contextid;
        print_object($sql);
//       return  $DB->count_records_sql($sql,$params);
      
      
        
         
        if (isset($options->contextarea) && !empty($options->contextarea)) {
            $params['contextarea'] = $options->contextarea;
        }
        if (isset($options->contextid) && !empty($options->contextid)) {
            $params['contextid'] = $options->contextid;
        }
        
        
        return $DB->count_records('block_mynotes',$params); 

    } */
    
  /*   public function count_notes($options) {
    	global $DB, $USER;
    	 
    	$params = array();
    	
    	$userid=$USER->id;
    	$contextarea='';
    	$courseid;
    	if (isset($options->contextarea) && !empty($options->contextarea)) {
    		$contextarea=$options->contextarea;
    	}
    	if (isset($options->courseid) && !empty($options->courseid)) {
    		$courseid=$options->courseid;
    	}
    	$param=array($userid,$contextarea,$courseid);
    	$sql='select count(contextarea) from {block_mynotes} where userid=? and contextarea=? and courseid=?';
    	 
    	$result=$DB->get_records_sql($sql,$param);
    	 
    	$count;
    	foreach ($result as $val){
    		$count=$val->count;
    		 
    	}
    	 
    	return $count;
    	//     	return $DB->count_records('block_mynotes', $params);
    } */
    

    public function count_mynotes($options) {
    	global $DB, $USER;
    	
    	$params = array();
    	$params['userid'] = $USER->id;
//     	$userid=$USER->id;
//     	$contextarea='';
//     	$courseid;
    	if (isset($options->contextarea) && !empty($options->contextarea)) {
    		$params['contextarea'] = $options->contextarea;
//     		$contextarea=$options->contextarea;
    	}
    	
    	if (isset($options->courseid) && !empty($options->courseid)) {
    		$params['courseid'] = $options->courseid;
//     		$courseid=$options->courseid;
    	}
//     	$param=array($userid,$contextarea,$courseid);
    	$sql='select count(contextarea) from {block_mynotes} where userid=? and contextarea=? and courseid=?';
    	
    	$result=$DB->get_records_sql($sql,$params);
    	
    	$count;
    	foreach ($result as $val){
    		$count=$val->count;
    	
    	}
    	
    	return $count;
//     	return $DB->count_records('block_mynotes', $params);
    }
    

    /*
     * Returns paging bar for mynotes
     * 
     * @param object $options must contain properties(contextid, count, page, perpage)
     * @return html
     */
    public function get_pagination($options) {
        global $OUTPUT;
        $baseurl = new moodle_url('/blocks/mynotes/mynotes_ajax.php', 
                array(
                    'contextid' => $options->contextid)
                );
        return $OUTPUT->paging_bar($options->count, $options->page, $this->perpage, $baseurl);
    }

    /*
     * Adding new record of mynote.
     * 
     * @return object of single mynote record if insert to DB else false
     */
    public function addmynote($context, $contextarea, $course, $content,$timer,$activityUrl,$activityname, $format = FORMAT_MOODLE) {
        global $CFG, $DB, $USER, $OUTPUT;
//         $content=$content.$timer;

        $newnote = new stdClass;
        $newnote->contextid = $context->id;
        $newnote->contextarea = $contextarea;
        $newnote->content = $content;
        $newnote->courseid = $course->id;
        $newnote->activityname = $activityname;
        $newnote->format = $format;
        $newnote->userid = $USER->id;
        $newnote->timecreated = time();
        $newnote->notestime=$timer;
        if (!empty($timer)){
//         	$s = 'Posted On April 6th By Some Dude';
//         	$variable= strstr($s, 'By', true);
        	
        $variable = substr($activityUrl, 0, strpos($activityUrl, "&start="));
        	
//         $newnote->notesurl=$activityUrl.'&resume='.$newnote->notestime.'&type=notes';
        
//         str_replace("start=","Peter",$activityUrl);
//         
//         $newnote->notesurl=$variable;
        
        if ($variable){
        	
        	$newnote->notesurl=$variable.'&start='.$newnote->notestime.'&type=notes';
        	
        }else{
        	
        	$newnote->notesurl=$activityUrl.'&start='.$newnote->notestime.'&type=notes';
        	
        }
        
        }else{
        $newnote->notesurl=$activityUrl;
        }

        if ($cmtid = $DB->insert_record('block_mynotes', $newnote)) {
            $newnote->id = $cmtid;
            $newnote->content = format_text($newnote->content, $newnote->format, array('overflowdiv' => true));
            $newnote->timecreated = userdate($newnote->timecreated, get_string('strftimerecentfull', 'langconfig'));
            $newnote->coursename = ($newnote->courseid == SITEID) ? '' : $course->fullname;
            $newnote->notestime = $newnote->notestime;
            $newnote->notesurl = $newnote->notesurl;
            if (!empty($newnote->coursename)) {
            	
            	if (!empty($newnote->notesurl)){
            		
            		      $newnote->coursename = html_writer::link($newnote->notesurl, $newnote->coursename);
            		}else {
            		
            			  $newnote->coursename = $newnote->coursename;
            			  }
            	
//             	$newnote->coursename = html_writer::link($newnote->notesurl, $newnote->coursename);
            	
//                 $newnote->coursename = html_writer::link($newnote->notesurl.'?notestime='.$timer, $newnote->coursename);
//                 $newnote->coursename=$newnote->notesurl.'?notestime='.$timer,$newnote->coursename;
            }
            return $newnote;
        } else {
            return false;
        }
    }

    /*
     * Find all available context areas which is used to store and retrieve mynotes.
     * 
     * @return array
     */
    public function get_available_contextareas() {
        return array(
//             'site' => get_string('site', 'block_mynotes'),
//             'course' => get_string('course', 'block_mynotes'),
//             'mod' => get_string('mod', 'block_mynotes'),
            'user' => get_string('user', 'block_mynotes'),
        );
    }
    /*
     * Find context area using context level.
     * 
     * @param object $context
     * @retrun string
     */
    public function get_current_tab($context, $page) {
    	
       /*  if ($page->url->compare(new moodle_url('/user/view.php'), URL_MATCH_BASE)) {
            return 'user';

        } else if ($page->url->compare(new moodle_url('/user/profile.php'), URL_MATCH_BASE)) {
            return 'user';

        } else if ($context->contextlevel == CONTEXT_SYSTEM) {
            return 'site';

        } else if ($context->contextlevel == CONTEXT_COURSE) {
            if ($context->instanceid == SITEID) {
                return 'site';
            }
            return 'course';

        } else if ($context->contextlevel == CONTEXT_MODULE) {
            return 'mod';

        } else if ($context->contextlevel == CONTEXT_USER) {
            return 'user';

        } else if ($context->contextlevel == CONTEXT_BLOCK) {
            $parent = $context->get_parent_context();

            if ($parent->contextlevel == CONTEXT_COURSE) {
                return 'course';
            } else if ($parent->contextlevel == CONTEXT_MODULE) {
                return 'mod';
            }
        } */
    	return 'user';
    }

    /**
     * Delete a note
     *
     * @param  int $mynoteid
     * @return bool
     */
    public function delete($mynoteid) {
        global $DB, $USER;
        if (!$mynote = $DB->get_record('block_mynotes', array('id' => $mynoteid))) {
            throw new mynotes_exception('deletefailed', 'block_mynotes');
        }
        if ($USER->id != $mynote->userid) {
            throw new mynotes_exception('nopermissiontodelete', 'block_mynotes');
        }
        return $DB->delete_records('block_mynotes', array('id' => $mynoteid));
    }
    
    /**
     * Edit a note
     *
     * @param  int $mynoteid
     * @return bool
     */
    public function edit($mynoteid,$notes) {
    	global $DB, $USER;
    	if (!$mynote = $DB->get_record('block_mynotes', array('id' => $mynoteid))) {
    		throw new mynotes_exception('editfailed', 'block_mynotes');
    	}
    	if ($USER->id != $mynote->userid) {
    		throw new mynotes_exception('nopermissiontoedit', 'block_mynotes');
    	}
    	$timecreated = time();
    	$param = array ($notes,$timecreated,$mynoteid );
    	$sql4 = 'update {block_mynotes} set content=? , timecreated=? where id=? ';
    	return $update_record = $DB->execute ( $sql4, $param );

    }
    
    public function all_note_edit($mynoteid,$notes){
    	global $DB, $USER;
    	
    	if (!$mynote = $DB->get_record('block_mynotes', array('id' => $mynoteid))) {
    		throw new mynotes_exception('editfailed', 'block_mynotes');
    	}
    	
    	if ($USER->id != $mynote->userid) {
    		throw new mynotes_exception('nopermissiontoedit', 'block_mynotes');
    	}
 
    	$param = array ($notes,$mynoteid );
    	$sql4 = 'update {block_mynotes} set content=? , timecreated=? where id=? ';
    	return $update_record = $DB->execute ( $sql4, $param );
    	
    }
    
    
}

/**
 * Mynotes exception class
 */
class mynotes_exception extends moodle_exception {
}